关闭DirectorySlash后避免目录列表

时间:2015-06-09 16:47:29

标签: php .htaccess mod-rewrite

我的网站文件结构如下:

.htaccess (No:1)
/application
/library
/public
    /static
    index.php
    .htaccess (No:2)

.htaccess(No:1)包含:

DirectorySlash Off
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.*[^/])$ $1/ #The step I tried to avoid directory Listing

RewriteRule ^$ public/   
RewriteRule ^(.*)$ public/$1  #Redirect everything to public folder

.htaccess(No:2)包含:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php
RewriteRule ^$ index.php #rewrite everything (if not a file) to index.php

当请求的网址是" / public"或" / library"或/ application"(请注意 DirectorySlash已关闭),它显示相应文件夹的目录列表。但是当添加尾部斜杠时,一切正常 - >它被重写为index.php并查找Controller。

我试图通过将此代码放在已经指定的.htaccess(No:1)上来避免引导列表:

RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.*[^/])$ $1/

这解决了"目录列表的问题"除了公共文件夹。

我想要的是解决目录lisitng的问题,当" / public"请求。我不想使用" deny from all"或者"选项FollowSymlinks"拒绝公共目录的目录列表,但将其重写为index.php

1 个答案:

答案 0 :(得分:1)

在root .htaccess中尝试此代码:

DirectorySlash Off
RewriteEngine On
RewriteBase /

RewriteRule ^(.*)$ public/$1 [L]

内部/public/.htaccess

DirectorySlash On
RewriteEngine On
RewriteBase /public/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]