如何将url http重写为https

时间:2014-09-20 06:47:07

标签: regex apache .htaccess mod-rewrite

我在.htaccess中使用此代码,但它无效。并且没有错误发生

 RewriteEngine On
    RewriteCond %{SERVER_PORT} ^80$
    RewriteRule ^.*$ https://%{localhost}%{REQUEST_URI} [R=301,L]

2 个答案:

答案 0 :(得分:1)

要将您的网址从http自动重写为https,只需执行此操作并将domain.com更改为您的域名TLD:

RewriteEngine On

RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://www.domain.com/$1 [r=301,nc,L]

答案 1 :(得分:1)

您使用%{localhost}是问题,因为没有名为%{localhost}的变量。

在根.htaccess或Apache服务器配置中尝试此规则:

RewriteEngine On

RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{ENV:protossl} !=s [OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !=on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]