简体   繁体   English

htaccess防止除index.php之外的直接.php文件

[英]htaccess prevent direct .php files except index.php

I want to prevent direct .php file access. 我想防止直接.php文件访问。 This below 2 lines works fine 这2行以下工作正常

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

But, it won't open index.php file. 但是,它不会打开index.php文件。 I want to prevent direct .php file access except index.php file 我想阻止直接的.php文件访问,但index.php文件除外

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

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

To prevent direct access to all .php files except index.php you can use this rule: 为了防止直接访问除index.php之外的所有.php文件,可以使用以下规则:

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

You need to use %{THE_REQUEST} variable for this. 您需要为此使用%{THE_REQUEST}变量。 THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. 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] 

Access to files "CSS" and "JS" and "index.php" will be there. 可以访问文件“ CSS”,“ JS”和“ index.php”。 Other requests will be redirect to "Deny" folders. 其他请求将重定向到“拒绝”文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM