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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 30 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
30 comments… add one
  • Local Service Quotes Jul 9, 2007 @ 6:05

    Thanks for the info and great site!

  • luca Aug 1, 2008 @ 7:19

    There actually is a little correction to be made.
    With the code you wrote, if you enter the site
    you are redirected to
    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 Feb 17, 2012 @ 12:15

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

    • Igor Nov 9, 2012 @ 18:58

      Good point – remove that slash.

  • S. Nilesh Aug 1, 2008 @ 16:11

    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 Aug 1, 2008 @ 16:27

    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 Jul 4, 2009 @ 14:28

    Nice tip, even nicer comments!

  • rebus Sep 12, 2009 @ 10:56

    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 Aug 24, 2011 @ 12:45

      if i do this but i have seen DNS 501 error

  • rebus Sep 12, 2009 @ 10:59

    heh, forgot it would strip vhost tags… :(

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

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

  • John Nov 25, 2009 @ 1:54

    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

      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. Oct 18, 2011 @ 10:28

      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 Jan 17, 2010 @ 5:39

    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 Jan 27, 2010 @ 7:55

    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

    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 Sep 27, 2010 @ 9:29

    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 Nov 10, 2010 @ 21:56

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

      my particular opinion.

      • Chris Aug 31, 2011 @ 20:21

        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 Dec 30, 2010 @ 6:37

    this site is very good !!

  • Andreas Apr 13, 2011 @ 14:55

    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)

  • Caue Rego May 2, 2011 @ 4:25
  • bastian Jul 17, 2011 @ 15:38

    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 Jul 23, 2011 @ 13:45

    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 Jul 24, 2011 @ 12:54


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

  • niraj Jul 28, 2011 @ 11:27

    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 Jul 31, 2011 @ 12:50

    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 Jul 31, 2011 @ 12:52

    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 @ 19:54

    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

  • Andy Hughes Dec 11, 2015 @ 9:28

    Add a “ServerAlias example.com” under the “ServerName http://www.example.com

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum