我目前正在使用它来通过htaccess强制https和www:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=302]现在,我想更改R=302中的R=301,并放弃域名本身。我不希望example.com出现在那里的原因可能是,对某些国家来说,域名在未来会有所不同。
所以我在想
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]..。但这将把www.example.com重定向到www.www.example.com,对吗?在这种特殊情况下,如何正确地做到这一点?我仍然想在一个单一的重定向中强制www和https。
发布于 2020-09-23 07:08:29
我仍然想在一个单一的重定向中强制www和https。
您需要像这样使用重定向规则来避免硬编码:
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]https://stackoverflow.com/questions/64022539
复制相似问题