htAccess 阻止直接.php文件,但索引.php除外


htaccess prevent direct .php files except index.php

我想阻止直接.php文件访问。下面的 2 行工作正常

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)'.php - [F,L]

但是,它不会打开索引.php文件。我想阻止除索引.php文件之外的直接.php文件访问

添加一个条件,不将规则应用于特定文件:

RewriteCond $1 !^(index'.php)$

要阻止直接访问除index.php之外的所有.php文件,您可以使用以下规则:

RewriteCond %{THE_REQUEST} '.php[?/] [NC]
RewriteRule !^index'.php$ - [F,L,NC]

为此,您需要使用%{THE_REQUEST}变量。 THE_REQUEST变量表示 Apache 从浏览器收到的原始请求,并且在执行某些重写规则后不会被覆盖。

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^index'.php$ - [F,L]
Options +FollowSymlinks
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !^(.+).css$ 
RewriteCond %{REQUEST_FILENAME} !^(.+).js$ 
RewriteCond %{REQUEST_FILENAME} !index.php$ 
RewriteRule ^(.+)$ /deny/ [nc] 

访问文件"CSS"和"JS"和"index.php"将在那里。其他请求将重定向到"拒绝"文件夹。