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:
blog.example.com -> /home/www/blogs/example.com
blog.blabla123.com -> /home/www/blogs/blabla123.com

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


Option 1: Brute Force

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

    ServerName blog.example.com
    DocumentRoot /home/www/blogs/example.com

    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /home/www/example.com/

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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