Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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
当API位于带有.htaccess的URL中时,是否重定向到不同的'index'文件?_.htaccess_Mod Rewrite - Fatal编程技术网

当API位于带有.htaccess的URL中时,是否重定向到不同的'index'文件?

当API位于带有.htaccess的URL中时,是否重定向到不同的'index'文件?,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我一直在努力将API调用重定向到不同的索引文件 我想将URL中包含[域名]/API/*的所有URL重定向到另一个脚本:API\u index.php 我在.htaccess文件中尝试了以下操作: RewriteEngine on RewriteCond %{REQUEST_URI} /API/ RewriteRule . API_index.php [L] RewriteRule ^(font-awesome|fonts|ajax|tools|log|documentation)($|/)

我一直在努力将API调用重定向到不同的索引文件

我想将URL中包含
[域名]/API/*
的所有URL重定向到另一个脚本:
API\u index.php

我在.htaccess文件中尝试了以下操作:

RewriteEngine on

RewriteCond %{REQUEST_URI} /API/
RewriteRule . API_index.php [L]

RewriteRule ^(font-awesome|fonts|ajax|tools|log|documentation)($|/) - [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav)$ index.php [NC,L]
根据这个URL,应该匹配,但我仍然被导航到index.php

回答

显然,.htaccess[L]只会停止URL上的当前迭代。 一旦我用我的第一条rewriteCond+规则重定向到API_索引文件,服务器就会用新的URL启动一个新的迭代,该URL将是
[some domain]/API_index.php

这被我的上一条规则捕获,并且仍然被重定向到
index.php

因此,该场景需要自己的规则:

RewriteRule ^API_index.php$ - [L]

整个文件如下所示:

RewriteEngine on

# Prevents redirecting API_index.php to index.php
RewriteRule ^API_index.php$ - [L]

RewriteCond %{REQUEST_URI} /API/
RewriteRule . API_index.php [L]

RewriteRule ^(font-awesome|fonts|ajax|tools|log|documentation)($|/) - [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav)$ index.php [NC,L]

非常感谢@starkeen的回答

这是因为
/API_index.php
与您的第三条规则匹配,并被重写为
/index.php

您需要原封不动地传递
/API_index.php
,尝试在
上重写引擎之后添加以下内容

RewriteRule ^API_index.php$ - [L]

还有其他规则吗?没有,这是所有的规则,但它不应该进入第三条规则,对吗?这就是为什么我把它放在后面。我尝试了你规则的几个位置,你能把整个.htaccess文件放在解决方案中,告诉我放在哪里吗?这是因为内部重定向,我不做你认为它做的,它只停止当前的迭代。例如,
/API/
被重写为
/API_index.php
,以便服务
/API_index.php
服务器再次读取htaccess,这次URI与您的上一条规则匹配。啊啊,这就是它的工作原理!谢谢你给我解释:D