Reputation: 409
I have recently set up SSL on a domain. This is on shared hosting which seems to be causing problems with redirecting the site to https
. I have asked previous questions regarding this problem without success. Any attempt seems to result in the site entering an infinite redirect loop. I have been given the following code by the service provide who host the site, which solve the problem of redirection, but not across all browsers. On Chrome, the code works as intended forwarding a http
page to its https
counterpart. However tests in Internet Explorer seem to forward all pages to the https
homepage and Firefox seems to just show an error screen. The code is as follows
<?php
if ($_SERVER['HTTP_X_FORWARDED_SSL'] == '1') { header("Location: $redirect"); } else { $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; header("Location: $redirect"); } ?>
Could anyone elaborate on this code to find a solution to this now long running problem.
Upvotes: 2
Views: 304
Reputation: 1020
use this code
$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https' : 'http';
Upvotes: 0
Reputation: 409
I have removed the header("Location: $redirect");
from the true command of the if
statement and the script seems to work on all browsers now.
<?php
if ($_SERVER['HTTP_X_FORWARDED_SSL'] == '1') { } else { $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; header("HTTP/1.1 301 Moved Permanently"); header("Location: $redirect"); } ?>
Upvotes: 0
Reputation: 21
Use the following code:
<?php
if(isset($_SERVER['HTTPS'])) {
$prefix = 'https://';
} else {
$prefix = 'http://';
}
$location = $prefix.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location: $location");
exit;
}
?>
Upvotes: 1
Reputation: 4275
I can't tell why its not working but try the code below to redirect http:// to https://
if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == ""){
$redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location: $redirect");
}
This will work on every browser.Hope this helps you
Upvotes: 1