Take the 2-minute tour ×

How to serve contents from different directory for virtual sub domains.

Ex: http://www.website.com gets its content from www directory.

for virtual.company.com i want to serve content from /www/application directory.

And i want to keep the url parameter same.

I have already created php files to display custom message based on the sub-domain., But i am having problems with htaccess,

In simple i just want to load content for subdomains from different directory other than the root directory.

share|improve this question
Guys, it's not just for 1 dub-domain, but for all sub-domains other than www, i..e for all sub-domains other than www i want to serve content from /www/application/ directory. –  Roccos Feb 4 '11 at 22:29

3 Answers 3

up vote 1 down vote accepted

Try this:

RewriteCond %{HTTP_HOST} !^www\.company\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.company\.com$ [NC] 
RewriteRule ^(.*)$ /application/%1 [QSA,L]
share|improve this answer
It's not just for virtual subdomain, it's for all subdomain other than www. –  Roccos Feb 4 '11 at 22:17
+1 Hmmmmm, that seems better than my solution. Still wouldn´t recommend it though... –  jeroen Feb 4 '11 at 22:20
@Roccos - updated to change for anything other than www. But would each subdomain/host have it's own directory, or are they all mapped to one directory? –  Eric Petroelje Feb 4 '11 at 22:22
They all are mapped to 1 directory, but the index page gets the subdomain name (via php functions) and displays the data accordingly (from the database). –  Roccos Feb 4 '11 at 22:25
It's just that i am going to provide a SAAS Solution, everything is ready and i want to provide custom url's to my clients but i don't know how htaccess works. –  Roccos Feb 4 '11 at 22:27

That doesn´t sound like a very good idea as people could enter http://www.website.com/application and that might lead to unwanted results. I would recommend setting up a virtual host for virtual.company.com and separate the two.

However, it is possible and it would be something like:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^virtual\.company\.com$ [NC]
RewriteRule ^(.*)$ http://www.company.com/application/$1 [R=301,L]
share|improve this answer
That is going to do a redirect though, which probably isn't what he wants. –  Eric Petroelje Feb 4 '11 at 22:19
@Eric Petroelje, yes, I just saw your answer and that seems better, I´m not that fluent in htaccess :-) –  jeroen Feb 4 '11 at 22:22

Setup a named virtual host.

<VirtualHost *:80>
  ServerName sub.domain.com
  DocumentRoot "/path/to/sub/domain"

<VirtualHost *:80>
  ServerName another.domain.com
  DocumentRoot "/path/to/another/sub/domain"
share|improve this answer

Your Answer


