开发者

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

开发者 https://www.devze.com 2023-01-28 04:29 出处:网络
i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that su开发者_StackOverflow中文版b-directory. I found several quite similar iss

i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that su开发者_StackOverflow中文版b-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]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号