2017-05-27 94 views
1

我想重定向所有的HTTP和HTTPS协议的要求,这些网址被重定向到https://www.example.com别名htaccess的通配符重定向HTTP和HTTPS请求

example.com 
example.com.au 
www.example.com.au 
example.co.nz 
www.example.co.nz 
example.co.uk 
www.example.co.uk 
example.net 
www.example.net 
example.net.au 
www.example.net.au 
example.org 
www.example.org 

我尝试这样做第一,但它不” t重定向https协议请求的网址。

RewriteCond %{HTTP_HOST} ^example.com [OR] 
RewriteCond %{HTTP_HOST} ^example.com.au [OR] 
RewriteCond %{HTTP_HOST} ^www.example.com.au [OR] 
RewriteCond %{HTTP_HOST} ^example.co.nz [OR] 
RewriteCond %{HTTP_HOST} ^www.example.co.nz [OR] 
RewriteCond %{HTTP_HOST} ^example.co.uk [OR] 
RewriteCond %{HTTP_HOST} ^www.example.co.uk [OR] 
RewriteCond %{HTTP_HOST} ^example.net [OR] 
RewriteCond %{HTTP_HOST} ^www.example.net [OR] 
RewriteCond %{HTTP_HOST} ^example.net.au [OR] 
RewriteCond %{HTTP_HOST} ^www.example.net.au [OR] 
RewriteCond %{HTTP_HOST} ^example.org[OR] 
RewriteCond %{HTTP_HOST} ^www.example.org 
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] 

我接下来尝试了这个,但它没有重定向其中有www的网址。

RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
RewriteCond %{HTTPS} !^on 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] 

如何使用,从所有别名域名在两个HTTP重定向URL的通配符和HTTPS来https://www.example.com

UPDATE:

重写规则的试验与!通配符按如下@CBroe评论:

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC,OR] 
RewriteCond %{HTTPS} on 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] 

更新2:

继@starkeen评论,因为我想要将http和https请求重定向到上述url,我在我的htaccess中添加了2套url。第一个是重写所有的HTTP到HTTPS,第二个重定向,如果它不符合上述网址:

# Force to use HTTPS 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] 

RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} !^www\.example\.com$ 
RewriteRule^https://www.example.com%{REQUEST_URI} [NE,L,R] 
+1

使用一个单一的条件来检查主机是_不是'www.example.com' ...? – CBroe

+0

这可能更容易。你能给我一个样品吗? – Neel

+1

你可以使任何RewriteCond模式与一个不匹配的模式进行预先匹配,使用'!',http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond – CBroe

回答

1

您可以使用此:

RewriteEngine on 


RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR] 
RewriteCond %{HTTPS} !on 
RewriteRule^https://www.example.com%{REQUEST_URI} [NE,L,R] 
+0

非常感谢。由于我希望重定向能够同时处理http和https请求,因此我添加了2条规则。第一个将http重定向到https,第二个重定向如果它不匹配url。我用新的重写规则更新了我的问题。你能否告诉我是否正确实施它?谢谢starkeen! – Neel

+0

此外,重写将永久301? – Neel

+1

尝试更新的规则,我已将您的2条规则合并到一条规则中。 R是临时重定向状态。将其更改为R = 301以使重定向永久。 – starkeen