.htaccess htaccess http到https重定向在url中应用两次www

wswtfjt7  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(141)

伙计们,我有一个小问题,迁移网站从HTTP到HTTPS。
我想强制所有到www https

  1. http://example.com-〉https://www.example.com(工作)
  2. http://www.example.com-〉https.www.example.com(不工作)
    上面的例子返回两个www.www的url
    https.www.www.example.com

我的代码

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我做错了什么?

fcy6dtqo

fcy6dtqo1#

您可以使用此重定向将http -> httpswww重定向合并到一个规则中:

# add www and turn on https in a single redirect rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]

验证R=302工作正常后,将其替换为R=301。测试mod_rewrite规则时,请避免使用R=301(永久重定向)。
另一方面,你的规则是在重定向后试图保持相同的方案(http或https)。你的规则也适用于www缺失的情况。你需要在两个条件之间使用OR,正如我在答案中所示。

相关问题