我有这个答案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版本? –