【发布时间】:2017-02-11 11:24:39
【问题描述】:
我需要为 mod rewrite URL 规则编写条件以检测正在执行的页面。
我使用的RewriteRule 工作正常,但问题是我需要对两个不同的 URL 使用相同的规则。
RewriteRule ^testing/([^/]*)$ /index.php?url=$1&name=&submit=submit [NC,L]
RewriteRule ^testing/([^/]*)$ /test.php?url=$1&name=&submit=submit [NC,L]
在没有设置任何重写条件的情况下,在浏览器中添加example.com/testing/ 时,无论我是在index.php 还是test.php,都会使用htaccess 中的第一个RewriteRule。
这是我尝试过的一些RewriteCond:RewriteCond %{REQUEST_FILENAME} /test.php$RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (test\.php)/ [NC]RewriteCond %{THE_REQUEST} \s/+(test\.php)/ [NC]RewriteCond %{THE_REQUEST} ^[A-Z]+\ /test\.php/
这是完整的 htaccess 文件代码:
Options +FollowSymLinks
RewriteEngine on
## check if on index.php ##
RewriteCond %{THE_REQUEST} ^.*\/index.php\/.*$ ## not working ##
## if on index.php do this ##
RewriteRule ^testing/([^/]*)$ /index.php?url=$1&name=&submit=submit [NC,L]
## check if on test.php ##
RewriteCond %{THE_REQUEST} ^.*\/test.php\/.*$ ## not working ##
## If on test.php do this ##
RewriteRule ^testing/([^/]*)$ /test.php?url=$1&name=&submit=submit [NC,L]
如何让第一个 (index.php) rewriteRule 在 index.php 上工作,但在 test.php 页面上时忽略第一个规则并使用第二个?
谢谢:)
编辑:只是为了澄清。我不想重定向。我只想根据我所在的页面更改testing/ 的值。正如我提到的 rewriteRule 已经可以正常工作了。
【问题讨论】:
-
你不能那样做。通过您的链接
/testing/...,无法知道它是针对index.php还是针对test.php。您可以使用代码或名称更改链接,例如testingi和testingt,或testing/i...等。 -
这就是条件应该是什么。这个想法是如果
index.php在脚本名称中,则执行规则,如果没有则不执行。如果test.php在脚本名称中,则执行该规则。如果没有,那就不要。 -
在
%{HTTP_REFERER}网址中?因为你重写了你的链接,当你使用它时,不是脚本名... -
不,我不想重写脚本名称。我想测试脚本名称是否包含字符串。如果是,则重写 URL。
-
你不懂... Apache 会在你使用时重写你的链接。不是在您运行脚本时。当用户单击链接时,您有时可能会在
%{HTTP_REFERER}中读取脚本名称(对不起,我的英语不好)
标签: php apache .htaccess mod-rewrite url-rewriting