≡ Menu

Apache redirect domain.com to www.domain.com

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.

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 29 comments… add one }

  • Local Service Quotes July 9, 2007, 6:05 am

    Thanks for the info and great site!

  • luca August 1, 2008, 7:19 am

    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

    • santosh February 17, 2012, 12:15 pm

      no no. Its working fine. The code is alright…

    • Igor November 9, 2012, 6:58 pm

      Good point – remove that slash.

  • S. Nilesh August 1, 2008, 4:11 pm

    If you have mod_alias enabled, then no need for Rewrites.

    Use this to redirect requests to domain.com to http://www.domain.com

    RedirectPermanent / http://www.domain.com

    This will Redirect http://domain.com/anything/foo/bar/baz to http://www.domain.com/anything/foo/bar/baz

    mod_alias is available in Apache 1.3 also.

  • S. Nilesh August 1, 2008, 4:27 pm

    Use mod_alias’s RedirectPermanent directive.

    RedirectPermanent / “http://www.domain.com/”

    it will redirect

    domain.com/foo/bar/baz to http://www.domain.com/foo/bar/baz

    mod_alias is available in Apache 1.3 also

  • George July 4, 2009, 2:28 pm

    Nice tip, even nicer comments!

  • rebus September 12, 2009, 10:56 am

    You should really setup another vhost that has servername domain.com and which redirects to http://www.domain.com/ and then another vhost for http://www.domain.com else you get stuck in infinite redirect loop.

    ServerName domain.com
    Redirect permanent / http://www.domain.com

    ServerName http://www.domain.com

    • guruprasad August 24, 2011, 12:45 pm

      if i do this but i have seen DNS 501 error

  • rebus September 12, 2009, 10:59 am

    heh, forgot it would strip vhost tags… :(

    <VirtualHost *:80>
    ServerName domain.com
    Redirect permanent / http://www.domain.com
    </VirtualHost>

    <VirtualHost *:80>
    ServerName http://www.domain.com
    </VirtualHost>

  • John November 25, 2009, 1:54 am

    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

    • Anonymous May 24, 2010, 3:48 am

      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

    • N.A. October 18, 2011, 10:28 am

      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).

  • Iguano January 17, 2010, 5:39 am

    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!

  • Patrik Lermon January 27, 2010, 7:55 am

    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]

  • SomeGuy May 14, 2010, 1:38 am

    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.

  • M van Zwijndregt September 27, 2010, 9:29 am

    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]

    • Roge November 10, 2010, 9:56 pm

      I think what you really need is just a alias
      server name domain.com
      server alias http://www.domain.com

      my particular opinion.

      • Chris August 31, 2011, 8:21 pm

        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.

  • rinshad December 30, 2010, 6:37 am

    this site is very good !!

  • Andreas April 13, 2011, 2:55 pm

    Hi!
    I have an Apache web server with >100 domains, with virtualdomainalias. I need a rewrite rule with regexp to reach the following (N,M=1-100):
    domain-N.tld -> http://www.domain-N.tld
    subdomain-M.domain-N.tld -> subdomain-M.domain-N.tld (and not to http://www.site.domain-N.tld)
    Thanx!
    Andreas

  • Caue Rego May 2, 2011, 4:25 am
  • bastian July 17, 2011, 3:38 pm

    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 !!

  • Perry July 23, 2011, 1:45 pm

    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]

  • Perry July 24, 2011, 12:54 pm

    Corrected:

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

  • niraj July 28, 2011, 11:27 am

    In http it works…. but

    I have tried the above for https but its not work.
    ewriteEngine on
    RewriteCond %{HTTP_HOST} ^domain\.com
    RewriteRule ^(.*)$ https://www.domain.com/$1 [R=permanent,L]

  • Danku July 31, 2011, 12:50 pm

    What is now the correct term to redirect websites from http:// to http://www?

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

  • Danku July 31, 2011, 12:52 pm

    We have a website hosted by Vodafone.
    The website is reachable under http://www.example.de but not http://www.example.de.
    I mailed the service team of Vodafone but got no really satisfying answer.

    How can we do that with htaccess-file?

  • Michael J May 21, 2012, 7:54 pm

    How would I redirect the domain but keep the URL ..
    example I want

    if someone goes to http://domain.com
    I want it to go to http://domain.com/test/index.html

    But want the URL to say domain.com

Leave a Comment