【问题标题】:Block index.php everywhere except in the root directory with .htaccess使用 .htaccess 在除根目录之外的任何地方阻止 index.php
【发布时间】:2015-05-08 14:23:39
【问题描述】:

我有一个这样的文件夹结构:

subdir
|_ subsubdir
  |_ index.php
  |_ other_stuff.php
  |_ images
    |_ pic.jpg
.htaccess
config.ini
index.php

.htacces 文件的内容:

Options +FollowSymLinks -MultiViews -Indexes

Order Deny,Allow
<FilesMatch "(\.ini$)|(\.php$)|(^\.)">
    Deny from all
</FilesMatch>
<Files index.php>
    Allow from all
</Files>

我的目标是阻止用户查看 subdir/subsubdir/index.php(以及所有其他 *.php 文件,无论在哪里,但这已经有效)但允许他们查看索引.php 在根目录中。

目前,用户显然仍然能够看到 subdir/subsubdir/index.php,因为所有名为“index.php”的文件都是允许的,但我不知道如何允许 just根目录中的一个并拒绝所有其他。我尝试过不同的事情,但要么完全拒绝,要么全部允许。

我觉得这应该是一件很容易的事,但我就是想不通。

我尝试过的事情:

<FilesMatch "\.\/index\.php">
    Allow from all
</FilesMatch>

---

<FilesMatch "^index\.php$">
    Allow from all
</FilesMatch>

---

<Files ./index.php>
    Allow from all
</Files>

---

<Files /index.php>
    Allow from all
</Files>

【问题讨论】:

    标签: php regex apache .htaccess webserver


    【解决方案1】:

    注释掉两个FilesMatch 块:

    并使用此代码:

    RewriteEngine On
    
    RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
    RewriteRule ^\.|\.(ini|php)$ - [F,NC]
    

    【讨论】:

    • 这行得通!没想到为此使用 mod_rewrite ......现在看起来很明显。感谢您的快速回答!
    猜你喜欢
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2015-03-28
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多