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 would like to setup a rule in Apache so that each time that a visitor gets redirected to my official domain (www.mydomaincom) whenever he's heading to Here's what I have set in Apache but it does not work:

<VirtualHost *:80>
    DocumentRoot c:/websites/
    CustomLog "c:/websites/" lscommon
    ServerAlias *
    RewriteEngine On
    <Directory />
        RewriteBase /
        RewriteCond %{HTTP_HOST} !^$ [NC]
        RewriteRule ^(.*)$$1 [L,R=301]

Anyone can help?

share|improve this question

I do the exact same thing, but in reverse:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*)$1 [R=Permanent]

Note: the [R=Permanent] flag uses a 301 Moved Permanently redirect.

So for you, that would be:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*)$1 [R=Permanent]

Secondly, take it out of the <Directory> tag.

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.