Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite domains to subdirectories transparently?

         

bakuretsu

7:36 pm on Dec 31, 2004 (gmt 0)

10+ Year Member



I have several domains that all point to the same physical web server. On the server, I have mod_rewrite capability through my root .htaccess (no access to httpd.conf).

What I want to do is have each of my domains' root directories actually live in a subdirectory of the site. In other words, when someone requests www.domain1.com, they should actually be retrieving files from www.domain1.com/domain1/, but I don't want them to KNOW this. Is that possible?

I want the user to see www.domain1.com, but every request to be transparently sent to www.domain1.com/domain1/, so that if they ask for www.domain1.com/foo.gif, Apache sends them www.domain1.com/domain1/foo.gif, and they don't know this is happening.

I have this, so far (one of numerous permutations I've tried):

RewriteCond %{HTTP_HOST} .*domain1.*
RewriteRule ^(/.*)?$ /domain1$1 [L]

There are two problems here. First, users see /domain1/ appended to their address bar. Second, if they request /foo.gif, they are sent to /foo.gif and the file doesn't exist (instead of being sent to /domain1/foo.gif, which at least would work).

Suggestions?

jdMorgan

8:07 pm on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



bakuretsu,

Welcome to WebmasterWorld!

The main problem is that in .htaccess context, the leading slash is stripped off the local URL-path variable "seen" by RewriteRule.

Also, there is the possibility that your code may be looping, so I'd suggest the following changes:

 
RewriteCond %{HTTP_HOST} ^(www\.)?domain1
RewriteCond %{REQUEST_URI} !^/domain1
RewriteRule (.*) /domain1/$1 [L]

If you are hosted on FreeBSD, you can use a more general solution that will rewrite *any* subdomain to a corresponding subdirectory. But it only works on servers with an OS that supports the latest version of posix regular expressions. See message #6 of this thread [webmasterworld.com].

Jim

bakuretsu

9:38 pm on Dec 31, 2004 (gmt 0)

10+ Year Member



Thanks jd!

I have tried what you suggested, but I still have the same two problems.

1. Visitors see /domain1/ added to the URL in their address bar (this was the major one that I wasn't sure was possible to avoid), and
2. A request for www.domain1.com/foo.gif fails to redirect to /domain1/foo.gif, and so I get a 404.

I have seen mod_rewrite used in the past to take a nested directory request (for example) and instead send the user the output of a script. Such as:

www.domain1.com/images/1045/

Internally, mod_rewrite changes that to:

www.domain1.com/view_image.php?image=1045

But the user only sees the first URL in their address bar, and in fact, the "images" directory isn't a real directory. That is what I want; I don't want the user to know that the files reside in a subdirectory on the server. Is that possible in this context?

Thanks for all your help.

jdMorgan

10:25 pm on Dec 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, well it's not your code doing this, unless you're not using what you posted. Either you have some other code that is interfering, or your host has code in the server configuration file that is doing it. Another possibility is that you've got some funky domain-forwarding setup, or some other factor you haven't mentioned.

Your code, and the modified version I posted do not invoke an external redirect. In addition, the code should rewrite a request for *any* resource in the "domain1" subdomain to the corresponding resource located in the associated /domain1 subdirectory. So there's no reason your image file should be 404.

This code is extremely simple, so if you're seeing the browser address bar update with the code I posted, you can be sure that there's some other agency involved.

Jim