防止访问除一个文件之外的所有 php 文件,而无需将文件放在单独的文件夹中


Prevent access to all php files besides one without putting files in a seperate folder

我正在使用以下代码来防止直接访问服务器上的.php文件:

#disable access to files by direct .php 
RewriteCond %{THE_REQUEST} ^[A-Z]+' /[^?' ]*'.php[/?' ]
RewriteRule .*'.php$ 404.php [L]

使用上面的代码段,任何访问我的服务器上的直接文件(例如index.php)的尝试都将被拒绝,而SEO链接不会被拒绝。

我想对服务器上名为"ajax.php"的 php 文件进行例外处理。如何在不将文件放在单独的文件夹中的情况下做到这一点?

您可以使用负面展望:

RewriteCond %{THE_REQUEST} 's/(?!ajax'.php).+?'.php['s?] [NC]
RewriteRule '.php$ 404.php [L]