我在.htaccess中使用此代码,但无法正常工作,并且未发生任何错误
RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{localhost}%{REQUEST_URI} [R=301,L]
最佳答案
您使用%{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]