Apache redirect domain.com to www.domain.com

Posted on in Categories , , , , , , , , last updated June 26, 2007

Q. What’s the best way to redirect domain.com to www.domain.com on the url address bar whenever someone omits or skip the www from domain.com? My site can be reached by two different URLs. For example http://domain.com/about/us.html and http://www.domain.com/about/us.html

How do I do configure Apache web sever vps for this task?

A. Apache (web server) 301 redirect is the most efficient and search engine friendly method for webpage redirection. You can place following code Apache’s httpd.conf vhost section or in .htaccess file.

$ vi .htaccess
Append following config code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L] 

Save and close the file. Above code will redirect users to www.domain.com url.

30 comment

  1. There actually is a little correction to be made.
    With the code you wrote, if you enter the site
    http://domain.com/index.php
    you are redirected to
    http://www.domain.com//index.php
    Notice the double slashes.

    The correction you need to do is on this line:

    RewriteRule ^(.*)$ http://www.domain.com$1 [R=permanent,L]

    just remove the slash before $1

  2. All this code looks nice, but it doesn’t help me any. If I knew this code and knew exactly where it goes, I wouldn’t be here. If you guys could explain this in detail it would help people like me. Like some installation instructions. To me, all you guys are doing is having a conversation amongst yourselves. Thank you

    1. john, don’t be so helpless – this site doesn’t exist to serve you

      either read up a little bit or pay someone to solve your exact problem

    2. you should have a file called httpd-vhosts.conf somewhere in your setup (inside the apache/conf/[something] directory most likely) that’ll have “” in it somewhere. It goes between the and tags. Add a second entry if you also use port 443 (https).

  3. If you are hosting multiple domains in a server, you might just add:

    ServerAlias domain.com

    in the .conf file of your domain. That’s it!

  4. The example provided will redirect visitors which are trying to reach domain.com to http://www.domain.com. But however, it won’t redirect other subdomains, for instance mistyped URL:S like wwww.domain.com or mail.domain.com.

    This example will redirect everything which is not http://www.domain.com to http://www.domain.com (provided that subdomains are pointing to the same place):

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

    And for those of you which want the opposite, i.e. that whatever the visitor writes he gets redirected to domain.com not using any subdomain, use this code:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^.*\.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

  5. I don’t understand the problem why, but the Redirect permanent solution is recommended for http to https around the net, too. However, it causes an infinite redirection loop, which is not what is needed! Rewrite rules work.

  6. If you’re using friendly urls, caught by a 404 handler like me, you can use the following code to append the friendly url to the new domain:


    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.mydomain.com%{REQUEST_URI} [R=301,L]

      1. This does not seem to be a good solution for wordpress sites that have already been configured where the full URL has already been stored in the database. This solution will cause the app to report database connection errors. I imagine that this would also affect other similar apps with mysql DB backend.

        Disclaimer, I’m not an Apache expert, and know little about mod_rewrite

        I think the best option is to re-write the URL when the client browser requests http://domain.com, and instruct the browser to request http://www.domain.com

        It seems like this is the best way to not break an app that connects to the database.

  7. Could you please add how can we do the for co.in and .co.cc like domains which will be more helpfull

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.domain\.co\.in$ [NC]
    RewriteRule ^(.*)$ http://www.domain.co.in$1 [L,R,QSA]

    To rewrite for multiple domains in one rule:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST

    Hope there is some mistake this will not work. please help to correct this !!

  8. Here is a more general rule that will map any *.com URL to www.*.com:

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

Leave a Comment