开发者

Apache mod_rewrite - forward domain root to subdirectory

开发者 https://www.devze.com 2023-01-03 02:51 出处:网络
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 stor

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_libr开发者_如何转开发ary/
        ...

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?


Finally managed to fix this myself. It turns out that Apache remembers folder settings even after the .htaccess file has been removed. So as I'd been playing with this for so long, some of my earlier attempts had completely polluted the config.

I solved it by first commenting all of the .htaccess file out apart from RewriteEngine off which put everything (almost) back to normal. Then I managed to get the desired effect by writing the following config:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$   www/   [L]
    RewriteRule (.*) www/$1 [L]
</IfModule>

Finally, I created another .htaccess file in the www directory containing:

<IfModule mod_rewrite.c>
    RewriteEngine off
</IfModule>

Now everything seems to work as I originally wanted. Shame it took so long to work all this out though!

0

精彩评论

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