仅允许通过.htaccess查看某些文件

时间:2013-10-27 11:07:28

标签: apache .htaccess mod-rewrite

我在服务器上有近20页,但我只想要一个名为abc.php的文件,用户可以观看。我想如果用户强行打开其他文件,如//example.com/some.php .htaccess显示403错误。

<Files ~ "^(?!(changepwd|login|register|remind|securitycode|registersuggest)\.php$).*(\.php)$">
AuthName "Reserved Area"
AuthType Basic
AuthUserFile path_to/.htpasswd
AuthGroupFile path_to/.htgroup
Order Deny,allow
Require group allowed_groups  
</Files>

这是我目前正在使用的方式,但我认为可以有更优雅的解决方案。

2 个答案:

答案 0 :(得分:20)

.htaccess - 文件只允许用户打开index.php。尝试访问任何其他文件将导致403错误。

Order deny,allow
Deny from all

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

如果您还想对某些文件使用身份验证,您可以在我的示例末尾添加当前文件中的内容。

答案 1 :(得分:1)

最安全地移动您不希望用户访问不在Web服务器根目录中的目录的文件。

就像我网站的根目录是这样的:

/var/www/html/my_web_site/public/index.php

我可以将非公共文件放在另一个目录中:

/var/www/html/my_web_site/config.php
/var/www/html/my_web_site/auth.php
/var/www/html/my_web_site/db.php

并在index.php中包含这样的文件:

include("../config.php");
include("../auth.php");
include("../db.php");

请注意,您不必冒险意外删除或忘记复制.htaccess文件。

相关问题