vhosting with mod_rewrite
September 15, 2005 3:30 PM   Subscribe

Newbie seeks compassionate mod_rewrite guru...

I'd like to do simple vhosting without physically changing the httpd.conf every time. I'm only looking to do ONE domain name (mydomain.net), but many virtual domains (user1.mydomain.net, user2.mydomain.net, etc.). I don't want to use mod_virtual_hosts because I want the option to do "regular" (non-mod_rewrite) virtual host setups that could use OTHER domain names in the future. So I need to use mod_rewrite, but I need to leave exceptions for icons, etc. (also an exception for /weblogs/ since Tiger Server is redirecting that to Blojsom (and Blojsom user) directories.

I'd like www.mydomain.net stuff to go to the usual webroot and so
I can put each vhost directory elsewhere (like beside the webroot - I'm open to suggestions).

I know mod_rewrite magic is suppose to be strong, so I'm sure this is do-able, but nobody in my office has worked with it. Any pointers down the right path would be appreciated.

I'm on OS X Tiger SERVER (but non-OS X-specific answers will still be helpful).
Apache 1.3.33
posted by spock to Computers & Internet (8 answers total)
 
I do almost exactly the same thing as you want, but I have it slightly differently. The relevant potion is:

RewriteEngine On
RewriteMap lowercase int:tolower

# Don't rewrite www.domain.com
RewriteCond %{HTTP_HOST} !^www.domain.com.*$

# Only rewrite for xxx.domain.com addresses
RewriteCond ${lowercase:%{HTTP_HOST}|NONE} ^(.+)$
RewriteCond %{HTTP_HOST} ^(.+).domain.com.*$
RewriteRule ^(.+)$ /home/$1/website/html-%1/$1

Working through these:

1. The rewrite engine is enabled.
2. We setup to allow ourselves to lowercase a domain name.
3. Only if the domain does not match www.domain.com do we perform a rewrite.
4. The domain name (HTTP_HOST, an Apache variable) is lowercased using our RewriteMap command.
5. We will only perform a rewrite if the domain matches something.domain.com, and we match the something, which goes into %1
6. The actual rewrite rewrites the document root to /home/%1/website/html/$1.

The %1 refers to the xxx part of the xxx.domain.com that we matched using line 5, and $1 refers to the rest of the URL (the page being accessed), so we preserve the page that we were trying to access before the rewrite.

This works for me, it should work for you, cut, paste and amend.
posted by gaby at 4:22 PM on September 15, 2005


Also, beware of Tiger server clobbering any changes that you make to the httpd.conf file by hand. It might mince them, it might not. I've not used Tiger server so I can't really say.
posted by gaby at 4:22 PM on September 15, 2005


Oh, to do your two excludes, for Blojsom and icons, etc, just have multiple RewriteCond fules:

RewriteCond %{REQUEST_URI} !^.+www.domain.com\/icons\/*$
RewriteCond %{REQUEST_URI} !^.+www.domain.com\/weblogs\/*$

It's all regexing really. I found most of this by trial and error, but see what works for you.
posted by gaby at 4:25 PM on September 15, 2005


Check out the URL Rewrite Guide. Specifically, the section titled "Virtual User Hosts". Looks like you can only do this with HTTP 1.1 because it mandates a Host header while HTTP 1.0 does not.
posted by sbutler at 4:26 PM on September 15, 2005


... also, there is a Mass Virtual Hosting section that looks a lot like gaby's solution.
posted by sbutler at 4:37 PM on September 15, 2005


Nowadays, every web browser you're likely to encounter is HTTP 1.1, so the lack of a Host header isn't going to be much of a problem.

The poor blighted HTTP 1.0 users that stumble upon your site will get directed to www.mydomain.net, so you could always provide some kind of alternate access to the virtual hosts from there (if you care).

You might want to check your access_log now to see how many unique clients have sent you HTTP/1.0 requests to decide whether catering to them would be worth the effort.
posted by nmiell at 5:35 PM on September 15, 2005


This was a link posted in a previous thread on mod_rewrite... http://www.ilovejackdaniels.com/cheat-sheets/mod_rewrite-cheat-sheet/

It's handy.
posted by SpecialK at 8:00 PM on September 15, 2005


Response by poster: Thanks to all for the responses, particularly the well commented mod_rewrite relevant rules. I'll try it and see how it goes!
posted by spock at 9:19 AM on September 16, 2005


« Older Good FREE websites/sources for comprehensive stock...   |   Outsourced VPN worth it? Newer »
This thread is closed to new comments.