Php htaccess:检查字符串是否在请求中的条件
我需要为mod rewrite URL规则编写一个条件,以检测正在执行的页面 我使用的Php htaccess:检查字符串是否在请求中的条件,php,apache,.htaccess,mod-rewrite,url-rewriting,Php,Apache,.htaccess,Mod Rewrite,Url Rewriting,我需要为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]
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/
时,无论我是在htaccess
还是test.php
上,都会使用htaccess
中的第一条重写规则
下面是我尝试过的一些重写条件:
RewriteCond%{REQUEST\u FILENAME}/test.php$
RewriteCond%{THE_REQUEST}^[A-Z]{3,9}\(test\.php)/[NC]
RewriteCond%{THE_REQUEST}\s/+(test\.php)/[NC]
RewriteCond%{u请求}^[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/
的值。正如我提到的,重写规则已经很好地工作了
## If on index.php ##
RewriteRule "^/index\.php$" "/index.php?url=$1&name=&submit=submit" [R,L]
试试这个
RewriteEngine On
#Checking if /index.php/ apears in URI
RewriteCond %{REQUEST_URI} ^(.*)\/index\.php\/(.*)$ [NC]
RewriteRule ^(.*)$ testing/index.php?url=$1&name=&submit=submit [NC]
这将显示index.php?url=sth1/index.php/sth2输出,而不是sth1/index.php/sth2
祝你好运对不起@Elby,我想你可能有点误解了。这条规则很管用。mod rewrite后的URL看起来像:example.com/testing/youtube.com
(youtube.com只是一个例子)我不想重定向。我需要根据我所在的页面更改testing/
的值。@turrican\u 34,你能用一个例子解释一下吗?我已经更新了完整htaccess代码中的注释。你不明白哪部分?@turrican\u 34,用户的具体请求是什么?您希望显示哪个文件?正如我提到的,我需要基本上更改testing/
的值。如果脚本名称/请求(条件)中有index.php
,则使用重写规则:rewriteRule^testing/([^/]*)$/index.php?url=$1&name=&submit=submit[NC,L]
。如果脚本名称/请求(条件)中有test.php
,则使用重写规则:“rewriteRule^testing/([^/]*)$/test.php?url=$1&name=&submit=submit[NC,L]”。您不能这样做。使用链接/testing/…
无法知道它是用于index.php
还是用于test.php
。例如,您可以使用代码或名称更改链接testingi
和testingt
,或testing/i.
等。这就是条件的作用。如果index.php
在脚本名中,则执行规则,如果不是,则不执行。如果脚本名称中有test.php
,则执行该规则。如果没有,那么不要。在%{HTTP\u REFERER}
url中?因为你重写了你的链接,当你使用它时,不是脚本名…不,我不想重写脚本名。我想测试脚本名是否包含字符串。如果是这样,请重写URL。你不明白。。。Apache在使用链接时重写它。当你运行你的脚本的时候。当用户单击链接时,您有时可能会在%{HTTP\u REFERER}
中读取脚本名称(对不起,我的英语不好)