2016-08-25 39 views
1

我有這個答案https://stackoverflow.com/a/16800319/1283556htaccess的跳過所有後面的規則子域匹配

我現在的.htaccess的301重定向問題向example.comhttps://www.example.com,在儘可能少重定向儘可能增加www.https://

我不希望它添加www.https://如果子域是m.example.com因此我認爲符合這個條件,並把無規則,使用[L]標誌停止做未來的情況會工作。

RewriteEngine On 

# Skip forcing www or https if the 'm' subdomain is being used for mobile 
RewriteCond %{HTTP_HOST} ^m\. [NC] 
RewriteRule - [L] 

# Force www. and HTTPS 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule^https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] 

# Force HTTPS (www. was already there) 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] 

# Don't remap resources to the index processor, just add SSL 
RewriteCond %{REQUEST_FILENAME} ^.*^.ico$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.css$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.js$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.gif$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.png$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.jpg$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.jpeg$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.xml$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.txt$$ 
RewriteRule ^(.*)$ https://$1 [R=301,L] 

# Remap /page/filter/?query to /index.php?page=page&filter=filter&query 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^/?([^/]+)/*([a-zA-Z0-9-]*)/?$ /index.php?page=$1&filter=$2 [QSA] 

的上述效果是:

example.com »301» https://www.example.com 
m.example.com »301» https://www.m.example.com 

所需的效果是:

example.com »301» https://www.example.com 
m.example.com (do nothing, just serve content from example.com/mobile) 

服務器版本:阿帕奇/ 2.4.10(UNIX)的OpenSSL/1.0.1e -fips mod_bwlimited/1.4阿帕奇

值得注意的是,我得到了這項工作荷蘭國際集團作爲一個解決方案,但我認爲這是一個變通,而不是線性解決方案:

RewriteEngine On 

# Force www. and HTTPS 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTP_HOST} !^m\. [NC] 
RewriteRule^https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] 

# Force HTTPS (www. was already there) 
RewriteCond %{HTTP_HOST} !^m\. [NC] 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L] 

# Don't remap resources to the index processor, just add SSL 
RewriteCond %{REQUEST_FILENAME} ^.*^.ico$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.css$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.js$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.gif$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.png$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.jpg$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.jpeg$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.xml$$ [OR] 
RewriteCond %{REQUEST_FILENAME} ^.*^.txt$$ 
RewriteRule ^(.*)$ https://$1 [R=301,L] 

# Remap /page/filter/?query to /index.php?page=page&filter=filter&query 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^/?([^/]+)/*([a-zA-Z0-9-]*)/?$ /index.php?page=$1&filter=$2 [QSA] 
+0

它應該工作。徹底清除瀏覽器緩存並重新測試。 – anubhava

+0

我的印象是,htaccess實際上並沒有發送到客戶端,所以實際上並沒有緩存,除非它是由服務器 - 但我從來沒有經歷過,不知道如何清除服務器的內部緩存(如果這確實是你的建議)。無論哪種方式,我清除了瀏覽器的緩存,但仍然重定向到https://www.m.example.com –

+0

什麼是Apache版本? –

回答

0

您的原始和更新的問題有不利於這兩個搜索引擎優化排名或用戶出現兩種301重定向規則。

可以結合兩個規則即http -> httpsforce-www成一個,也採取相同的規則排除m.主機的護理:

RewriteEngine On 

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

請務必保持此規則的第一個規則在完全清除瀏覽器緩存後進行測試。

+1

我的版本從來沒有做過兩個301的 - 但這看起來很整潔,我會給它一個鏡頭。我擔心,如果你去http:// www.example,你會被重定向到https:// www.www.example - 但會檢查。謝謝 –

+0

實際上,如果您發送請求的URL爲:'http:// example.com',那麼您的版本將執行兩次301重定向。第一次301到'https:// example.com',第二次301到'https:// www.example.com'。瀏覽器緩存可能不會顯示,但如果你從命令行卷曲測試,那麼它將兩者都。 – anubhava

+0

但是,如果您訪問https // example.com,那麼使用我的第301條規則,它會在第一個規則集中一次性添加https和www,並且不會執行兩次。我使用301重定向服務進行了測試,以確保它對搜索引擎友好性好。 –