Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

In my web app, I'm toying with the idea of giving my user the opportunity to select a subdomain of their choosing, so they could select something like:

where foobar is their chosen subdomain. What is the proper way to go about setting up something like this? .htaccess? Have some api for writing virtual hosts? The application would still always map to one directory on my sever, I just want to give theme a custom URL.

share|improve this question
up vote 3 down vote accepted

Yes, it can be done through .htaccess but it will only work if you have a wildcard DNS entry for the domain or you also add a DNS record for each sub-domain.

share|improve this answer

I would set up an API that writes virtual hosts as you suggest, just make sure it makes the appropriate checks to make sure nothing dodgy happens; such as multiple VH with the same name, naming conventions etc...

share|improve this answer

i think the mod_rewrite way is more intended to serve different directories, so i'd rather handle it inside the application itself. After all you're talking just about URL rewriting within the same application. A wild card DNS record would still be needed, tho.

I wouldn't even bother with a vhost api.

Either way, mind of course that using HTTP HOST header to select the content served has security implications, like any other user provided input.

share|improve this answer

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.