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

Add a comment

Leave a Reply

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

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement