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. i.domain.com -> domain.com/i/ 2. js.domain.com -> domain.com/js/ 3. css.domain.com -> domain.com/css/

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.

migrated from stackoverflow.com 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 a.domain.com to domain.com/a , you don't need the .htaccess. Just change the DocumentRoot of your subdomain a.domain.com pointing to /var/www/vhosts/domain.com/httpdocs/a . 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/domain.com/httpdocs/a

Now upload this file to /var/www/vhosts/domain.com/subdomains/subdomain/conf 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.


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/domain.com/. 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: https://stackoverflow.com/questions/4593978/redirecting-to-multiple-virtual-subdomains-using-htaccess

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 i.mydomain.com so I just replicated it for c.domain.com and js.domain.com

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.