Jak*_*fin 1 php apache .htaccess mod-rewrite
我一直在努力寻找一种使用.htaccess强制在我的网站上使用HTTPS的方法。我尝试过的任何方法都会导致重定向循环。我找到了一个PHP替代方案,并对其进行了修改以适合我的Web服务器,但是如果有人可以帮助,我希望使用.htaccess版本。
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] != "https") {
header("HTTP/1.1 301 Moved Permanently");
$location = "https://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
header("Location: $location");
exit;
}
Run Code Online (Sandbox Code Playgroud)
您可以在根.htaccess中使用此规则:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301,NE]
Run Code Online (Sandbox Code Playgroud)