【问题标题】:htaccess skip all following rules for subdomain matchhtaccess 跳过所有以下子域匹配规则
【发布时间】: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


【解决方案1】:

您的原始问题和更新问题有两个 301 重定向规则,这对 SEO 排名或用户体验都不利。

您可以合并这两个规则,即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]

确保将此规则保留为第一条规则,并在完全清除浏览器缓存后对其进行测试。

【讨论】:

  • 我的版本从来没有做过两个 301——但这看起来很整洁,我会试一试。我担心如果你去 http:// www.example 你会被重定向到 https:// www.www.example 虽然 - 会检查。谢谢
  • 实际上,如果您发送 URL 为:http://example.com 的请求,那么您的版本将执行两次 301 重定向。第一个 301 到 https://example.com 和第二个 301 到 https://www.example.com。浏览器缓存可能不会显示,但如果您从命令行 curl 进行测试,那么它会同时显示。
  • 但是如果你去 https//example.com 使用我的第一个 301 规则,它会在第一个规则集上一次添加 https 和 www,并且不会执行两次。我使用 301 重定向服务进行了测试,以确保它对 SEO 友好
  • 是的,但这已经是 https URL。但是,如果您发送http://example.com,则会有两个重定向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 2011-03-30
  • 2012-06-11
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 2010-09-26
相关资源
最近更新 更多