Apache mod_rewrite - forward domain root to subdirectory

Posted by DuFace on Stack Overflow See other posts from Stack Overflow or by DuFace
Published on 2010-06-13T13:40:06Z Indexed on 2010/06/13 13:42 UTC
Read the original article Hit count: 220

Filed under:
|

I have what I originally assumed to be a simple problem. I am using shared hosting for my website (so I don't have access to the Apache configuration) and have only been given a single folder to store all my content in. This is all well and good but it means that all my subdomains must have their virtual document root's inside public_html, meaning they effectively become a folder on my main domain.

What I'd like to do is organise my public_html something like this:

public_html/
    www/
        index.php
        ...
    sub1/
        index.php
        ...
    some_library/
        ...

This way, all my web content is still in public_html but only a small fraction of it will be served to the client. I can easily achieve this for all the subdomains, but it's the primary domain that I'm having issues with.

I created a .htaccess file in public_html with the following:

Options +SymLinksIfOwnerMatch # I'm not allowed to use FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/www [NC]
RewriteRule ^(.*)$ /www/$1 [L]

This works fairly well, but for some strange reason www.example.com/stuff is translated into a request for www.example.com/www/stuff and hence a 404 error is given. It was my understanding that unless an 'R' flag was specified, mod_rewrite was purely internal so I can't understand why the request is generated as that implies (to me at least) redirection.

I assumed this would be a trivial problem to solve as all I actually want to do is forward all requests for the root of www.example.com to a subdirectory, but I've spent hours searching for answers and none are quite correct. I find it difficult to believe I'm the only person to have this issue.

I apologise if this question has been answered on here before, I did search and trawl but couldn't find an appropriate answer. Please could someone shed some light on this?

© Stack Overflow or respective owner

Related posts about apache

Related posts about mod-rewrite