toggle menu

Need 301 redirection from all version to https with www

Rajnish Kumar |07 Aug at 05:08

Hi anyone's,

I want to implement 301 redirection  http both version, index.php and https non www version to https www version.

Please suggest me!!

Regards

Rajnish

  • 1 like
  • 2 comments

Dhananjay Kumar10 Jan at 05:01

Hi,

Please try the below script , its work on all :

For www only :

if ($_SERVER['HTTP_HOST']=='xyz.com') {
$url = "http://www.". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header( "HTTP/1.1 301 Moved Permanently" );
header("Location: {$url}");
exit;
}

With HTTPS & WWW :

if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" || $_SERVER['HTTP_HOST']=='domainname.com') {
$url = "https://www.". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header( "HTTP/1.1 301 Moved Permanently" );
header("Location: {$url}");
exit;
}

Try and suggest.

Dhananjay Kumar10 Jan at 05:01

Also, you may use this code

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
header( "HTTP/1.1 301 Moved Permanently" );
header("Location: {$url}");
exit;
}

Posted:

07 Aug at 05:08

Viewed:

2548 times

Active:

10 Jan at 05:01