我有這個答案https://stackoverflow.com/a/16800319/1283556htaccess的跳過所有後面的規則子域匹配
我現在的.htaccess的301重定向問題向example.com
https://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]
它應該工作。徹底清除瀏覽器緩存並重新測試。 – anubhava
我的印象是,htaccess實際上並沒有發送到客戶端,所以實際上並沒有緩存,除非它是由服務器 - 但我從來沒有經歷過,不知道如何清除服務器的內部緩存(如果這確實是你的建議)。無論哪種方式,我清除了瀏覽器的緩存,但仍然重定向到https://www.m.example.com –
什麼是Apache版本? –