htaccess拒绝访问index.php作为目录索引

时间:2017-06-13 21:50:54

标签: php apache .htaccess

我的.htaccess文件:

DirectoryIndex index.php

order allow,deny
<FilesMatch "^(index|testfile)\.php$">
Allow From All
</FilesMatch>

拒绝访问站点root,这通常会打开index.php。加载网站的唯一方法是转到mysite.com/index.php而不是mysite.com如果我删除order它按预期工作,但现在该目录中的所有文件都可以访问。

如何配置htaccess以拒绝除index.php以外的所有文件,但也允许导航到mysite.com而不是mysite.com/index.php

1 个答案:

答案 0 :(得分:1)

你可以这样做:

DirectoryIndex index.php
order Deny,Allow

# deny everything except landing page
<FilesMatch ".">
Deny From All
</FilesMatch>

# allow index.php and testfile.php
<FilesMatch "^(index|testfile)\.php$">
Allow From All
</FilesMatch>

替代解决方案,使用mod_rewrite

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !\.(?:css|js|png|jpe?g|gif|ico|tiff)$ [NC]
RewriteRule ^(?!(index|testfile)\.php). - [F,NC]