2017-01-09 35 views
2

我試圖找出一個.htaccess規則,我可以在我的主域&子域名上使用,而不需要更改每個子域的代碼。動態強制HTTPS和非WWW的子域名

我試過以下的子域嘗試和強制https和非www,但它不能正常工作。

<IfModule mod_rewrite.c> 
    # Force HTTPS & NON-WWW 
    RewriteEngine On 
    RewriteCond %{HTTP_HOST} ^www\.test\.website\.com$ [NC] # Detect if it has www? 
    RewriteCond %{HTTPS} !=on [OR] 
    RewriteCond %{HTTP_HOST} !^test\.website\.com$ [NC] 
    RewriteRule^https://test\.website\.com%{REQUEST_URI} [R=301,L] 
</IfModule> 

目前,如果我是去http://www.test.website.com/test/它重定向到https://test%2Cwebsite.com/所以它有點工作,但不完全是。

我一直在試圖做更多的動態也使用:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteCond %{HTTPS} !=on [NC] 
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L] 
</IfModule> 

但是這是行不通的。達到這個目標的最佳解決方案是什麼?

應該基本上可以強迫所有如下:

http://website.com/     => https://website.com/ 
http://www.website.com/    => https://website.com/ 
https://www.website.com/    => https://website.com/ 

http://website.com/testing/   => https://website.com/testing/ 
http://www.website.com/testing/  => https://website.com/testing/ 
https://www.website.com/testing/  => https://website.com/testing/ 

http://test.website.com/    => https://test.website.com/ 
http://www.test.website.com/   => https://test.website.com/ 
https://www.test.website.com/   => https://test.website.com/ 

http://test.website.com/testing/  => https://test.website.com/testing/ 
http://www.test.website.com/testing/ => https://test.website.com/testing/ 
https://www.test.website.com/testing/ => https://test.website.com/testing/ 
+1

「它重定向到'https:// test%2Cwebsite.com /'」 - 有一些奇怪的事情發生在那裏,它不涉及您在上面發佈的指令。 '%2C'是一個編碼的逗號?!那來自哪裏? – MrWhite

+0

@ w3dk我也無法理解%2C,網址中沒有逗號,但它正在放置一個。 – Ryflex

回答

3

您可以使用此單一動態規則實現所有http -> httpswww -> non-www重定向:

RewriteEngine On 

RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://%1%{REQUEST_URI} [R=301,L,NE] 

確保這條規則是正確的在頂部和在測試此更改之前清除瀏覽器緩存。

+2

我*仍然*從您的重寫中學習。 – sepehr

+0

是否需要第三個'RewriteCond'指令?如果您對前兩個進行重新排序幷包含捕獲組,則第一條規則是什麼:'www www。(。+)'? – MrWhite

+2

@ w3dk是的,需要在可選的'www'之後捕捉零件。這兩個條件,之前使用'OR'子句,因此只有一個將被執行,我們不能從中捕獲任何東西。 – anubhava

1

你重寫規則必須(在第一個例子):

RewriteRule ^/(.*) https://test.website.com/$1 [R=301,L] 

刪除反斜槓,重寫目標是沒有正則表達式。

對於您的整體解決方案,我想您需要兩次單獨重寫。一個用來執行https檢查,另一個用來刪除www。我想這在一次重寫中是不可能的,因爲你需要檢查兩個條件,其中只有一個可能匹配。

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

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

試試這個:

Options +FollowSymLinks 

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteBase/

    # First, force the HTTPS, whatever it is: 
    RewriteCond %{HTTPS} off 
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

    # Then drop the www, if any: 
    RewriteCond %{HTTP_HOST} ^www\.(.*) 
    RewriteRule .* https://%1%{REQUEST_URI} [L,R=301] 
</IfModule> 
## Results: 
# http://website.com/  => https://website.com/ 
# http://www.website.com/ => https://website.com/ 
# https://www.website.com/ => https://website.com/ 

# http://website.com/testing/  => https://website.com/testing/ 
# http://www.website.com/testing/ => https://website.com/testing/ 
# https://www.website.com/testing/ => https://website.com/testing/ 

# http://test.website.com/  => https://test.website.com/ 
# http://www.test.website.com/ => https://test.website.com/ 
# https://www.test.website.com/ => https://test.website.com/ 

# http://test.website.com/testing/  => https://test.website.com/testing/ 
# http://www.test.website.com/testing/ => https://test.website.com/testing/ 
# https://www.test.website.com/testing/ => https://test.website.com/testing/