【发布时间】:2017-01-01 17:43:26
【问题描述】:
我对这个答案有疑问https://stackoverflow.com/a/16800319/1283556
我当前的 HTACCESS 301 将example.com 重定向到https://www.example.com,并在尽可能少的重定向中添加www. 和https://。
如果子域是m.example.com,我不希望它添加www. 或https://,所以我认为匹配此条件并且不设置任何规则,并使用[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)
服务器版本:Apache/2.4.10 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Apache
值得注意的是,我将此作为解决方案,但我认为它是一种变通方法,而不是线性解决方案:
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]
【问题讨论】:
-
它应该可以工作。彻底清除浏览器缓存并重新测试。
-
我的印象是 htaccess 并没有真正发送到客户端,因此实际上没有缓存,除非它是由服务器 - 但我从来没有经历过,也不知道如何清除服务器的内部缓存(如果这确实是您的建议)。无论哪种方式,我都清除了浏览器的缓存,但它仍然重定向到 m.example.com
-
什么是 Apache 版本?
-
服务器版本:Apache/2.4.10 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 Apache
标签: apache .htaccess redirect mod-rewrite