php - Mod_Rewrite 规则冲突让我发疯

标签 php apache .htaccess mod-rewrite

我有一个 .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/

相关文章:

php - Paypal IPN 初学者 - 根本无法使用

.htaccess - 为什么此 RedirectMatch 301 会替换 & 符号?

.htaccess - 使用 htaccess 转换 url ...删除和添加单词

PHP:删除所有未按预期运行的 fcn,代码内部

php - 如何将 UnixDate 转换为 mysql_format 或 php

apache - fsimage hdfs 的内容

Apache重写规则: How to check for host name in cookies?

node.js - 如何安全地将 Drupal 连接到 Nodejs socket.io

.htaccess - 将混合内容重定向到 HTTPS 的正确 htaccess 规则是什么

php - OCI8 + PHP7.1 + Windows10 : The specified module could not be found