Rewrite rule to hide folder, doesn't work right without trailing slash

i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.

My .htaccess looks like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ foo/$1 [QSA,L]

The document-root only contains the following folder/files:

/foo/bar/index.html

I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.

Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.

When i put something this

RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteCond %{REQUEST_URI} !^[^.]*.html$
RewriteCond %{REQUEST_URI} !^[^.]*.php$
RewriteRule ^(.*)$ $1/ [QSA,L]

on top of that rule it seems to work, but that would require me to list every used file extension...

Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.

Thanks in advance!


1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.

just idea, this has not been tested.


Better late than never...

Got it working with a simple RewriteRule which append a / to every url that doesn't have on.

# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]
链接地址: http://www.djcxy.com/p/36536.html

上一篇: .htaccess文件不存在时重定向

下一篇: 重写规则以隐藏文件夹,如果不使用斜线,则无法正常工作