htaccess的重定向到https:// WWW重定向、htaccess、WWW、https

2023-09-02 00:18:05 作者:尋歡

我有以下的htaccess code:

I have the following htaccess code:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

我希望我的网站重定向到的https:// WWW 但是当我访问 HTTP:// WWW 不重定向我的https:// WWW

I want my site to be redirected to https://www. but when I access http://www. does not redirect me to https://www.

推荐答案

要率先发力HTTPS,必须检查正确的环境变量%{HTTPS}关闭,但你的排除以上则prepends的 WWW。既然你有第二个规则执行 WWW。,不使用它的第一个规则。

To first force HTTPS, you must check the correct environment variable %{HTTPS} off, but your rule above then prepends the www. Since you have a second rule to enforce www., don't use it in the first rule.

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]