Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 mydomain.com. Here's what I have set in Apache but it does not work:

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

Anyone can help?

share|improve this question

1 Answer 1

I do the exact same thing, but in reverse:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*) http://example.com/$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 (.*) http://www.example.com/$1 [R=Permanent]

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

share|improve this answer

Your Answer

 
discard

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.