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 am trying to map several subdomains to directories in my ROOT using .htaccess (is 'map' the right word for it?)

The server uses Plesk, which is something Im not particulary familiar with (cPanel would have handled this automatically). All subdomains have been added as "Domain Aliases", so all I need it to do now is point to the right directory.

eg: 1. -> 2. -> 3. ->

Ive tried many snippets I found in Google and specifically Stack Overflow. This is the code that got me the closest by working for (1), but inexplicably failing for (2) + (3) by still pointing to the ROOT without looking in a directory.

RewriteCond %{HTTP_HOST} ^i\.domain\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} ^js\.domain\.co\.uk$ [NC]
RewriteCond %{HTTP_HOST} ^css\.domain\.co\.uk$ [NC]
RewriteRule (.*) %1/$1 [L]

I confess I'm not particularly up on .htaccess or how DNS works, PHP is my game.

Any help, greatly welcomed.

share|improve this question

migrated from Sep 25 '11 at 12:37

This question came from our site for professional and enthusiast programmers.

I have it working now, posted my answer below SparX. Pretty sure SparX answer is spot on too, just not quite what I needed - so if you're looking for a similar solution, his might still fit your own needs. – AndyH Sep 26 '11 at 9:22

Well, Plesk handles things differently so you have to do things, the Plesk way ;)

The document root of a subdomain in Plesk will be something like,


As in your question, to point to , you don't need the .htaccess. Just change the DocumentRoot of your subdomain pointing to /var/www/vhosts/ . To do this, you need to add a vhost.conf to the conf directory of subdomain with the new DocumentRoot settings.

Create a file named vhost.conf with the following contents,

DocumentRoot /var/www/vhosts/

Now upload this file to /var/www/vhosts/ folder.

Now run the following command to include the custom conf along with the other httpd includes.

/usr/local/psa/admin/bin/websrvmng -a

Restart apache, and confirm whether the subdomain is now serving the pages from correct location.

PS: Sometimes the Plesk version matters (for the commands you execute), so feel free to ask if you get any troubles setting this up.

share|improve this answer

Thanks for your answer SparX - I'm sure it's spot on, you clearly know your stuff.

My host had advised me against using the standard subdomain directory structure in Plesk due to me needing RWX permissions across most subdomains from the main domain document_root.

This meant I had to remove pretty much all open_basedir restrictions above /var/www/vhosts/ I didn't realise I was causing some massive security problems - the naivity of us PHP developers!

So they advised I added my subdomains into "domain aliases", move the directories into the main domain's document_root and use .htaccess to rewrite the requests.

After a full weekend of hair pulling, I found this answer on Stack overflow:

Which lead me to using the following in my main domain's .htaccess:

# Internal rewrite for the image subdomain
#  - Match the subdomain exactly
RewriteCond %{HTTP_HOST} ^i\.mydomain\.com$
#  - Check to see if the rewrite already happened (prevent
#    infinite loop of internal rewrites)
RewriteCond %{REQUEST_URI} !^/i(/.*|$)
#  - Rewrite the URL to the subdirectory
RewriteRule ^(.*) /i/$1 [L]

That took care of so I just replicated it for and

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.