我很难让多个 mod_rewrite 规则在我的 .htaccess 文件中协同工作。在整个网站中,我想删除“www”。来自所有 URL。我在文档根目录中使用以下内容:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301]
然后,在一个文件夹“/help”中我想做 2 次重写:
- 更改域名.com/help/1todomain.com/index.php?question=1
- 更改domain.com/help/category/exampletodomain.com/index.php?category=示例
所以在domain.com/help 中我有以下内容:
Options +FollowSymLinks
RewriteRule ^([0-9]+)/?$ index.php?question=$1 [NC,L]
RewriteRule ^category/([^/\.]+)/?$ index.php?category=$1 [NC,L]
上述 2 个 .htaccess 文件适用于:
www.domain.comto域名.com
域名.com/help/1todomain.com/index.php?question=1
域名.com/help/category/exampletodomain.com/index.php?category=示例
但是,当我需要结合两次重写以删除“www”时,这不起作用。并将子文件夹重写为 url 变量。例如。:
www.domain.com/help/1todomain.com/index.php?question=1
给出 500 错误。
我哪里做错了?而且,这是否最好使用 2 个 .htaccess 文件,或者是否可以/应该将这 2 个文件合并到文档根目录下的 1 个 .htaccess 文件中?
看起来发生的事情是 .htaccess 文件中的规则/help文件夹正在被应用,因为您正在请求该文件夹中的某些内容,因此不会应用父文件夹的规则。如果您添加RewriteOptions Inherit
在你的/help文件夹的.htaccess:
Options +FollowSymLinks
RewriteOptions Inherit
RewriteRule ^([0-9]+)/?$ index.php?question=$1 [NC,L]
RewriteRule ^category/([^/\.]+)/?$ index.php?category=$1 [NC,L]
但是,继承的规则可能不会按照您期望的顺序应用。例如,如果您请求http://www.domain.com/help/1/ http://www.domain.com/help/1/你最终会被重定向到http://domain.com/index.php?question=1 http://domain.com/index.php?question=1如果您试图通过隐藏查询字符串来创建 SEO 友好的 URL,这可能不是您想要的。
你最好的选择可能是把东西搬到/help文件夹到文档根目录中,以便您可以控制规则的应用顺序:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301]
RewriteRule ^help/([0-9]+)/?$ /index.php?question=$1 [NC,L]
RewriteRule ^help/category/([^/\.]+)/?$ /index.php?category=$1 [NC,L]
这确保重定向到非 www 域首先发生,那么/help规则得到应用。所以当你去http://www.domain.com/help/1/ http://www.domain.com/help/1/,您首先被重定向到http://domain.com/help/1/ http://domain.com/help/1/然后应用帮助规则并将 URI 重写为/index.php?question=1
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)