mod rewrite Force HTTPS + WWW



我目前正在使用以下方法来强制www.但是我也想强制https

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

我将如何正确编辑它以强制https以及www.

修复了主站点:

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

其他域现在被迫包含https我只希望我的主站点这样做......

主站点的不同修复程序:

RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^DOMAIN.com$ [NC]
RewriteRule ^.*$ https://www.DOMAIN.com%{REQUEST_URI} [R,L]

似乎工作正常。我已将其他域移至其他服务器

下面是一个规则,可用于在同一规则中强制wwwhttps

# https and www in one rule
RewriteCond %{HTTP_HOST} !^www. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]

RewriteCond的排序在这里非常重要%1否则将无法正确捕获。前 2 个条件一起或OR,第 3 个条件AND ed。

最新更新