只允许使用.htaccess索引访问

我有这个在我的.htaccess文件,在网站的根:

Options -Indexes <directory ../.*> Deny from all </directory> <Files .htaccess> order allow,deny deny from all </Files> <Files index.php> Order allow,deny allow from all </Files> 

我试图实现的是阻止文件夹和文件访问不被称为index.php,不pipe哪个目录被访问。 我有文件夹部分工作完美,拒绝从所有规则工作以及 – 但我试图允许访问index.php失败。

也许你想复杂,只是拒绝所有访问,然后允许访问index.php和文件夹本身。 例如:

 Order allow,deny Deny from All <FilesMatch "^(index\.php)?$"> Allow from All </FilesMatch> 

如果你甚至不想要http://doma.in/folder/工作,你可以&#x7528;Files index.phpreplaceFilesMatch指令。 请记住,它取决于您的DirectoryIndex指令访问没有文件名的文件夹时将显示什么文件。

编辑:要回答你的评论中的问题,我认为你将需要一个更强大的.htaccess使用mod_rewrite。 根据Apache文档,您不能在.htaccess文件中使用Directory和DirectoryMatch。 因为在RewriteRule可以添加结尾的斜杠之前,请求被拒绝,所以不能使用mod_write添加结尾的斜杠。

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !/index\.php$ RewriteRule ^.*$ - [F] </IfModule> <IfModule !mod_rewrite.c> Order allow,deny Deny from All </IfModule>