Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php htaccess:检查字符串是否在请求中的条件_Php_Apache_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

Php htaccess:检查字符串是否在请求中的条件

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]

我需要为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/
时,无论我是在
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}
中读取脚本名称(对不起,我的英语不好)