1

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!

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

^(.*)/?$ would make the trailing slash optional FYI
0

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

just idea, this has not been tested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.