Php 阻止直接访问URL中的文件

Php 阻止直接访问URL中的文件,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,在/hello/bar中有一个文件foo.php 所以如果用户说 /hello/bar/foo.php#禁止访问 /hello/bar/foo#重定向到foo.php 到目前为止,我所做的是,在/中的.htaccess中 RewriteEngine on RewriteRule "^hello/bar/foo$" "hello/bar/foo.php" [L] RewriteRule "^hello/bar/foo.php$" "-" [F] 但这似乎不起作用。放置两个URL后,服务器将发送4

在/hello/bar中有一个文件foo.php

所以如果用户说

/hello/bar/foo.php#禁止访问

/hello/bar/foo#重定向到foo.php

到目前为止,我所做的是,在
/
中的.htaccess中

RewriteEngine on
RewriteRule "^hello/bar/foo$" "hello/bar/foo.php" [L]
RewriteRule "^hello/bar/foo.php$" "-" [F]

但这似乎不起作用。放置两个URL后,服务器将发送403错误。如何更正此问题?

一旦应用了一组重写规则,结果将返回给URI解析器,如果URI发生更改,该解析器将重新调用重写规则集(在相同的.htaccess中或不同的.htaccess中,取决于新的URI)

这意味着,您使用的
[L]
标志并没有完全停止重写处理,它只是停止通过规则集处理当前周期

因此,在.htaccess中,第一条规则将
hello/bar/foo
重写为
hello/bar/foo.php
,并且
[L]
结束当前的重写。URI解析器会在URI发生更改后从顶部再次调用重写。第一条规则这次不适用,但第二条规则导致禁用(又名HTTP 403-禁用)

所以,你需要一种方法来阻止这种循环。在Apache的更高版本中,您可以使用
[end]
标志而不是
[L]
。这将彻底结束所有重写

RewriteEngine on
RewriteRule "^hello/bar/foo$" "hello/bar/foo.php" [END]
RewriteRule "^hello/bar/foo.php$" "-" [F]

这有什么意义?您将再次重定向到拒绝访问的页面,因此您将永远无法访问任何链接。否。只能使用
foo
访问文件
foo.php
,而不能使用
foo.php
。当然,一定有办法做到这一点。您是在尝试这样做,以便.php不会出现在URL中,还是foo和foo.php应该是两个独立的文件?你的问题不是很清楚。