Redirect a site to www using PHP

To redirect a site to URL with www using PHP, you can use the following PHP code

<?php

$domainName = $_SERVER['HTTP_HOST'];

if (strpos($domainName, 'www') === false) {
    header("HTTP/1.1 301 Moved Permanently");
    $urlNew = 'https://www.' . $domainName .  $_SERVER['REQUEST_URI'];
    Header("Location: $newUrl");
    exit;
}

Add this code in your index.php file or another file included by index.php

See Redirect

Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *