Apache .htaccess - 使用“.php”而不是“index.php”阻止所有网址

时间:2017-01-07 15:45:46

标签: php regex apache .htaccess mod-rewrite

我喜欢阻止我的网站的所有“.php”请求,除了“index.php”我怎么能用.htacess和重写mod实现这个?

RewriteEngine On
RewriteCond %{REQUEST_URI} ^.*(.php) [NC]
RewriteRule ^(.*)$ - [F,L]

2 个答案:

答案 0 :(得分:1)

您可以在RewriteRule中使用否定条件模式:

RewriteEngine On

RewriteCond %{REQUEST_URI} \.php$ [NC]
RewriteRule !^index\.php$ - [F,NC,L]

答案 1 :(得分:0)

我用了很多年,它完美无缺。您可以添加多个文件以允许列表以|

分隔
#PROTECT FILES
<Files *.php>
Order Deny,Allow
Deny from all
</Files>

#ALLOW FILES
<FilesMatch index.php|captcha.php>
Allow from all
</FilesMatch>