Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否使用.htaccess阻止对文件和子目录的访问?_Php_.htaccess - Fatal编程技术网

Php 是否使用.htaccess阻止对文件和子目录的访问?

Php 是否使用.htaccess阻止对文件和子目录的访问?,php,.htaccess,Php,.htaccess,我有将近30个php文件,在我的目录中有4个子目录。我想阻止用户直接查看一些php文件和子目录,如http://bhavani.com/hai.php 我的当前htaccess文件 ## Enable Mod Rewrite, this is only required once in each .htaccess file RewriteEngine On RewriteBase / ## Test for access to includes directory RewriteCond

我有将近30个php文件,在我的目录中有4个子目录。我想阻止用户直接查看一些php文件和子目录,如
http://bhavani.com/hai.php

我的当前
htaccess
文件

## Enable Mod Rewrite, this is only required once in each .htaccess file
RewriteEngine On 
RewriteBase / 
## Test for access to includes directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /includes/ .*$ [NC] 
## Test that file requested has php extension 
RewriteCond %{REQUEST_FILENAME} ^.+\.php$ 
## Forbid Access 
RewriteRule .* - [F,NS,L]

怎么做?

我认为您只能使用RewriteRule而不能使用RewriteCond 例如:
RewriteRule^/hai\.php-[F,NS,L]
此规则禁止访问hai.php文件
对于其他文件,您可以使用其他规则或使用掩码。

您可以像这样筛选文件

<Files ~ "\.php$">
  Deny from all
</Files>
<Directory "/subdirectory">
  Deny from all
</Directory>

全盘否定
像这样的目录

<Files ~ "\.php$">
  Deny from all
</Files>
<Directory "/subdirectory">
  Deny from all
</Directory>

全盘否定

您不需要
mod\u rewrite
来完成此操作。一种更简单的方法是使用
mod_alias
中的
RedirectMatch
指令:

RedirectMatch 403 ^.*/include/*\.php$

这将自动对
include
子目录中任何PHP文件的直接请求做出
403禁止
响应,但是您仍然可以从其他PHP文件中
包含它们。

您可以在PHP文件的开头创建一个简单的if()语句,如用户登录或任何您需要的访问权限,如果没有,则标题(“404”);出口它会删除所有文件。但是我想要一些像索引这样的文件。php@jayamalladi,为
index.php