2017-05-27 40 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

相關問題