首页 > 解决方案 > 如果通过 htaccess 具有特定路径,请不要重定向到 www

问题描述

我的 .htacess 文件中的以下规则,对于我的 Drupal 网站,将所有非 www URL 重定向到 www 版本。(例如:https://example.com-> https://www.example.com):

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这很好用,但现在我想排除https://example.com/samltest/test重定向到 www。我添加了以下内容:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !/samltest/ [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但是现在当我到达时https://example.com/samltest/test,它会重定向到https://example.com/index.php. 有人对我做错了什么有任何想法吗?

标签: apache.htaccessdrupal

解决方案


在 .htaccess 的顶部有这样的内容:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{THE_REQUEST} !\s/+samltest [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

清除浏览器缓存后进行测试。


推荐阅读