Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I gave localhost an alias: 'myhost'. Entering 'myhost' in my address bar shows me /var/www/index.php. So far so good. Set up an apache virtual host on /var/www and called it 'myhost', then added three aliases: '*.myhost' '*.myhost.* 'myhost.*' the idea being that it would work like so:

myhost = site index/landing page/general stuff

user.myhost = general stuff specific to the user

myhost.topic = topical stuff not specific to a user

user.myhost.topic = topical stuff specific to a user

but all the data is loaded dynamically so the actual urls would be more like:

myhost/index.php?user=(empty or name of user)&host=myhost&app=(empty or topic)

I've spent two days trying to figure out the rewrite rules for this and no joy. Any help would be greatly appreciated.

EDIT: Also, do I have to add user1.myhost, user2.myhost, myhost.topic1, myhost.topic2, user1.myhost.topic1, user2.myhost.topic1, user1.myhost.topic2, user2.myhost.topic2, etc to my hosts file to make this work? If so, isn't there a better way?

share|improve this question
up vote 2 down vote accepted

You can use these rewrite rules to redirect all domains to myhost with a host parameter.

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} myhost
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^.+$
RewriteRule ^(.*)?(.*)$ $1?$2&host=%{HTTP_HOST} [L]

RewriteRule ^(.*)$ http://myhost/$1?host=%{HTTP_HOST} [L]

You can also get domain name in your app (for example $_SERVER["SERVER_NAME"] in php) and use this value. So you dont need an extra host parameter for it.

You can install an easy-to-use dns server (like tinydns) and set wildcard subdomains like *.myhost etc. But user2.myhost.topic2 is not a valid domain. If your domain is "", you subdomains should be "" or "". You cant use "user1.example.topic1" as a domain in internet.

share|improve this answer
I have a feeling this is very close, but it isn't working. I think it is looking for a subdomain folder, but I'm not using any. All content is served through a single file: /var/www/index.php. Basically, I want the url to reflect the change in content without refreshing the page (I'm using jquery for content fetching), or to load up the appropriate content (based on the query string) depending on what url is typed straight into the address bar. – bee.catt Aug 1 '12 at 15:41
So, for example: 'myhost' is '/index.php?user=&host=myhost&app=' while 'me.myhost.stuff' is '/index.php?user=me&host=myhost&app=stuff' – bee.catt Aug 1 '12 at 15:44
Sorry, i misunderstood your problem. So my first answer is irrelevant. – dirigeant Aug 1 '12 at 16:10
you can always edit it if you know a better solution. It's given me something to work with anyway, so if I don't get a better answer in the next few days, I'll accept your answer. Thank you for your effort. – bee.catt Aug 2 '12 at 1:47
I think, you can set wildcard subdomains (on dns) and wildcard virtual host (in web server) and send all requests to same "index.php" file. Then you can get domain/subdomain in your php app and get your data dynamically. You dont need any extra parameter for user/topic etc. and you dont need any rewrite to do this. – dirigeant Aug 2 '12 at 14:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.