htaccess跳过以下所有子域匹配规则

时间:2016-08-25 10:26:33

标签: apache .htaccess redirect mod-rewrite

我对此答案有疑问https://stackoverflow.com/a/16800319/1283556

我当前的HTACCESS 301将example.com重定向到https://www.example.com,并在尽可能少的重定向中添加www.https://

如果子域名为www.,我不希望它添加https://m.example.com,因此我认为符合此条件并且没有规则,并使用{{1标志停止做未来的条件会起作用。

[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

服务器版本:Apache / 2.4.10(Unix)OpenSSL / 1.0.1e-fips mod_bwlimited / 1.4 Apache

值得注意的是,我将此作为一种解决方案,但我认为这是一种解决方案而非线性解决方案:

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

1 个答案:

答案 0 :(得分:0)

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

您可以将<{1}}和http -> https两个规则合并,并且负责在同一规则中排除force-www个主机

m.

确保将此规则作为首要规则,并在完全清除浏览器缓存后对其进行测试。