I am trying to redirect only the index page of my website to HTTPS version using the following code but it gives domain.com redirected you too many times
ie ERR_TOO_MANY_REDIRECTS
error. There are no redirect codes in htaccess
except the 4XX & 5XX error redirections.
if($_SERVER['HTTPS'] !== "on")
{
$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
header("Location:$redirect");
}
How to redirect only one page to HTTPS without affecting other URLs in PHP?
Your problem is in this line
if($_SERVER['HTTPS'] !== "on")
Per the manual
'HTTPS' : Set to a non-empty value if the script was queried through the HTTPS protocol.
So just use
if(!$_SERVER['HTTPS'])
Here is my code for doing redirect for non www pages. You can adapt it easily for https.
// redirect to www if necessary
$kc_ur_pos = stripos($_SERVER['HTTP_HOST'],'www.');
if ($kc_ur_pos === false)
{ $kc_ur='https://www.';
$kc_ur .= $_SERVER['HTTP_HOST'];
$HTTPURI = $kc_ur . $_SERVER['REQUEST_URI'];
header("HTTP/1.1 301 Moved Permanently"); // Optional.
header("Location: $HTTPURI");
exit;}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.