.htaccess阻止除root之外的所有访问(使用或不使用尾部斜杠)

时间:2013-07-17 15:41:19

标签: apache .htaccess

我的方法是 - 拒绝除了我熟悉的所有内容。

我希望允许访问rootindex.phpfamiliar 'safe' extensions

因此,我不必考虑我应该保护什么,而是我不应该这样做。

这是我的.htaccess文件的外观:

# Disable files and directories index listing
Options -Indexes
DirectoryIndex index.php

# Deny all files.
<FilesMatch ".">
    Order Deny,Allow
    Deny from all
</FilesMatch>

# Except index.php
<FilesMatch "index.php$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

# Except root
<FilesMatch "^$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

# And except familiar extensions
<FilesMatch "\.(css|js|jpe?g|png|gif)$">
    Order Allow,Deny
    Allow from all
</FilesMatch>

除非我尝试访问没有尾部斜杠的地址,否则一切都会正常运行:http://localhost/site而不是http://localhost/site/, 然后我得到'禁止'错误页面。

非常感谢一些帮助。

2 个答案:

答案 0 :(得分:3)

回复旧帖子,但万一其他人徘徊在这里,您可以修改您的FilesMatch,如下所示:

<FilesMatch "^(/)?$">

这匹配位置的根,有或没有尾部斜杠。

答案 1 :(得分:2)

通过拒绝仅访问包含点.的文件来解决此问题:

# Deny all files.
<FilesMatch "\.">
    Order Deny,Allow
    Deny from all
</FilesMatch>