Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a tricky problem (for me at least).

I'm trying to rewrite / to a folder /webroot/www. I have some simple code and it works:

RewriteRule ^$ /webroot/www/ [L]

However at the same time if the URL starts with components, followed by anything else (ex. foo, as in /components/foo), and foo is an actual directory that exists inside components, I should rewrite to /components/foo/www instead.

How can I achieve that? I can't seem to figure it out.

I'm using Apache with .htaccess.

share|improve this question

1 Answer 1

I believe this should do it to at least some extent:

RewriteEngine   On
# rewrite components/.../ -> components/.../www
RewriteCond %{REQUEST_URI} "^/components/"
RewriteCond %{REQUEST_URI} "!^/components/.*/www"
RewriteRule "components/(.*)/" "/components/$1/www" [L]
# if the rewritten directory doesn't exist, restore 
RewriteCond %{REQUEST_URI} "^/components/.*/www"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "components/(.*)/www" "/components/$1" [L]
RewriteRule ^$ "webroot/www" [L]
share|improve this answer
    
Hi, por. No luck unfortunately... Maybe I could rewrite / to /webroot/www, and block everything else unless it's /components/.*/www... –  jason Oct 9 '12 at 0:04
    
Where does it fail for you? In my tests it works as defined. –  por Oct 9 '12 at 5:51
    
Hi, por. I don't know, it didn't do the redirect. Now I have this: RewriteEngine on RewriteCond %{REQUEST_URI} !/components/.*/www RewriteRule components/(.*)/ components/$1/www/ [L] # rewrite webroot RewriteCond %{REQUEST_URI} !/webroot/www RewriteCond %{REQUEST_URI} !/components/ RewriteRule (.*) webroot/www/$1 [L] [continues...] –  jason Oct 9 '12 at 12:14
    
...everything works, so if I go to / it rewrites to /webroot/www, if I go to /components/Foo/ rewrites to /components/Foo/www, which is good. However, I am still able to browse directories outside /webroot/www and /components/Foo/www. Also, I can visit /webroot/www and it looks the same as /, while it should still rewrite to /webroot/www/webroot/www... –  jason Oct 9 '12 at 12:16
    
So you want two additional things: 1) All other urls (other than /webroot/www and /components/.../www) to result in 404 HTTP Not found? 2) /webroot/www should also redirect like /? Is this really intended? –  por Oct 9 '12 at 16:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.