简体   繁体   English

.htaccess强制HTTPS

[英].htaccess force HTTPS

I've been struggling for a while to find a method of using .htaccess to force HTTPS on my website. 我一直在努力寻找一种使用.htaccess强制在我的网站上使用HTTPS的方法。 Any way I've tried has resulted in a redirect loop. 我尝试过的任何方法都会导致重定向循环。 I found a PHP alternative and amended it to suit my webserver, but I'd like the .htaccess equivalent if anybody could help. 我找到了一个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;
}

You can use this rule in your root .htaccess: 您可以在根.htaccess中使用此规则:

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301,NE]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM