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

is there a way to redirect all specified subdomains to a specific local folder? like this:

I want to attach a subdomain to a local folder but the domain is the wildcard (can be anything) this domain will be used as a folder name. example:

blog.* -> /home/www/blogs/*

so it goes like this: -> /home/www/blogs/ -> /home/www/blogs/

I am using apache2, so I can edit the configs or create a .htaccess? any ideas? Thanks.

share|improve this question
up vote 2 down vote accepted

Option 1: Brute Force

For domain VirtualHost you set up in Apache, set up a corresponding VirtualHost for the blog. Something like this:

    DocumentRoot /home/www/blogs/

    DocumentRoot /home/www/

Option 2: Dynamic Virtual Hosting

The mod_vhost_alias module for Apache allows for Dynamcially configured mass virtual hosting, which allows you to set up one single configuration set that is used for multiple virtual hosts, where everything is identical from domain to domain except the directory where the files will be found.

The secret sauce of dynamic virtual hosting is the VirtualDocumentRoot directive. It looks something like this:

UseCanonicalName Off
VirtualDocumentRoot /var/www/%0/public_html/

Which might correspond to --


Check out the documentation for UseCanonicalName to see why it should be present, and directory name interpolation to see what %0 means, and whether you might want to put something else there instead.

Option 3: Rewrite Rules

Mod_rewrite allows you to supply a set of translation rules which tell Apache to turn one url into another, and then re-route the request to wherever that URL might take it.

With it, you can do pretty much anything, but it takes some mental gymnastics sometimes to understand what you're telling the server to do. I'm not going to go too deep into how to make it work because writing rewrite rules without a solid understanding of how they work is a path that ends ultimately in madness. Best to learn before you leap.

At least skim the mod_rewrite manual, have a good look over the reference page, and read the examples and make sure you understand how they work.

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.