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 have written a RewriteRule which works on the basis of HTTP_HOST (, but I want it to work on the basis of the domain name part only (

To clarify further

The folder structure is /var/www/

When I write this rule in apache conf file

RewriteRule ^/js/(.*) /%{HTTP_HOST}/js/$1 [L]

and access the site using -- it tries to find the folder /var/www/, which does not exist.

So, I need to convert the above-mentioned rule to remove the "www" from the HTTP_HOST.

share|improve this question
I gave an answer based on your rewrite-rule, but I am not sure if rewriting is the right way to go (see pulegium's answer about symlinks). Maybe you could clarify on what you want to achieve? There might be a simpler solution. – touchstone Jan 24 '10 at 13:43
up vote 3 down vote accepted

I would suggest that you redirect requests rather than make the same site available in two places. Either create a new VirtualHost to redirect traffic:

<VirtualHost *:80>
    Redirect /

Or add a set of rules above your others that will detect the presence of the www. prefix and redirect the user:

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule /(.*) http://%1/$1 [R,L]

If you really want your site on both domain names without any redirection, you can use a RewriteCond to pull out just the domain name, and it will be available to the RewriteRule as %1.

RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*) [NC]
RewriteRule ^/js/(.*) /%1/js/$1 [L]
share|improve this answer

I doubt if you can do that, wouldn't it be easier just to create links to appropriate directories?

ln -s
share|improve this answer

Have you tried using %{DOCUMENT_ROOT}?

Also possible, if you have set Servername in your vhost to the same name you use in your directory structure, eg:

<VirtualHost *:80>
  DocumentRoot /var/www/

Then you can use %{SERVER_NAME} in your rewrite-rule.

share|improve this answer

as i understand you question, just set your DocumentRoot to the right directory:

DocumentRoot /var/www/
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.