【问题标题】:Block access to files and sub directory with .htaccess?使用.htaccess 阻止对文件和子目录的访问?
【发布时间】:2012-11-16 08:41:58
【问题描述】:

我的目录中有近 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]

怎么做?

【问题讨论】:

  • 您可以在 php 文件的开头创建一个简单的 if() 语句,例如用户登录或访问它们所需的任何内容,如果没有,则 header("404");退出;

标签: php .htaccess


【解决方案1】:

你可以像这样过滤文件

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

还有这样的目录

<Directory "/subdirectory">
  Deny from all
</Directory>

【讨论】:

  • 它拒绝所有文件。但我想要一些文件,如 index.php
  • @jayamalladi,为index.php创建另一个类似于上面的规则
【解决方案2】:

您不需要mod_rewrite 来完成此操作。一个更简单的方法是使用来自mod_aliasRedirectMatch 指令:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-04-22
      • 1970-01-01
      • 2023-04-01
      • 2011-09-16
      • 2011-09-26
      • 2014-03-31
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      相关资源
      最近更新 更多