htaccess合并301 www和https

问题描述 投票:1回答:2

下面的代码将非www重定向到www,然后转发到https://

如何将这些合并为一个,以便我不会有双重定向。

# Redirect non www to www
RewriteCond %{HTTP_HOST} ^mysite.nl$ [NC]
RewriteRule (.*) http://www.mysite.nl/$1 [R=301,L] 

# Redirect non https to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
.htaccess redirect mod-rewrite url-rewriting
2个回答
1
投票

您可以使用OR条件合并两个规则:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^mysite.nl$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

'ornext | OR'(或下一个条件)使用此命令将规则条件与本地OR组合而不是隐式AND。

参考:http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond


1
投票

尝试以下规则,

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^mysite.nl$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
© www.soinside.com 2019 - 2024. All rights reserved.