View Full Version : Mod rewrite - creating fake subdomains

11-13-2007, 09:52 PM
I was wondering if someone can help me out with a small issue I am having with mod rewrite; I have experience rewriting URL's but am having trouble with this one..

Basically what I want to do is this..

username.domain.com = www.domain.com/users/username/

Also.... any other page that the user requests on the sub-domain would redirect to the appropriate page... such as..

username.domain.com/page1.html = www.domain.com/users/username/page1.html
username.domain.com/page2.html = www.domain.com/users/username/page2.html
username.domain.com/blah/something.html = www.domain.com/users/username/blah/something.html

..& so on..

Obviously I still want the subdomain "www" going to the main site & not to be re-written.

Thanks for any help! :)

11-13-2007, 11:00 PM
First of all, do you have control over your server? can you modify/create your own virtual host containers?

Second, do you have wild card DNS set up?

Doing dynamic sub-domains is fairly strait forward once you have that set up.


11-13-2007, 11:07 PM
Yes..... have got wildcard domains setup.

can you modify/create your own virtual host containers?

Yes.... have control over my server; but not quite sure what you mean by this?

11-13-2007, 11:15 PM
There are a probably a few ways this can be accomplished.
This is the way I have done it in the past.

I created 2 vhosts. the first serves my default site (www) and the other
gets all wild card entries.

From there, you can look at the SUBDOMAIN and do what ever you like with it.

Here is a quick example I came up with.
There is some obvious info missing like logs, error docs, etc.

<VirtualHost *>
DocumentRoot /var/www/htdocs/default
ServerName your.tld
ServerAlias www.your.tld

<VirtualHost *>
DocumentRoot /var/www/htdocs/dynamic
ServerName *.your.tld
ServerAlias *.your.tld

#this sets an environment variable for use with php
#php example: $_SERVER["SUBDOMAIN"];
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(([a-zA-Z0-9_]+)\.)?your\.tld(:[\d]+)?$ [NC]
RewriteRule .* - [env=SUBDOMAIN:%2]