我有一个 .htaccess 如下,有些规则是冲突的 - 我需要以某种方式制定一个条件,这样它们就不会发生
RewriteEngine On
RewriteRule ^search$ results_template.php [L]
RewriteRule ^load-([a-z0-9-]+)$ index.php?key=$1 [L]
RewriteRule ^([a-z0-9-]+)$ http://domain.com/share/index.php?key=$1 [L]
.htaccess 文件位于 http://domain.com/directory/.htaccess
第一条规则应该只是将名称 search
重写为 results_template.php
并停止处理更多规则
http://domain.com/directory/search
负载
http://domain.com/directory/results_template.php
第二条规则应该从
中获取abcdef-1
http://domain.com/directory/load-abcdef-1
并将其作为参数发送给
http://domain.com/directory/index.php?key=abcdef-1
然后停止处理规则。
第三条规则应该从
中获取abcdef-1
http://domain.com/directory/abcdef-1
并将其作为参数发送给
http://domain.com/share/index.php?key=abcdef-1
然后停止处理规则。
我遇到的问题是,每当我请求 http://domain.com/directory/search 时,无论我如何尝试让它们很好地协同工作。第三条规则被调用,我被重定向。
我确信这只是完全的疲倦或完全的愚蠢,但我已经在这一小步骤上花费了太多时间,所以任何帮助都会很好。谢谢
最佳答案
您将需要 RewriteConds:
RewriteEngine On
RewriteBase /directory
RewriteRule ^search$ results_template.php [L]
RewriteRule ^load-([a-z0-9-]+)$ index.php?key=$1 [L]
RewriteCond %{QUERY_STRING} !^results_template.php
RewriteRule ^([a-z0-9-]+)$ http://domain.com/share/index.php?key=$1 [L]
例如用户请求/search,然后查询将转换为 results_template.php。使用这个新查询/results_template.php 再次检查所有规则,当然最后一个规则适合,因为它是最通用的规则。
关于php - Mod_Rewrite 规则冲突让我发疯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9850338/