Apache redirect domain.com to www.domain.com

by on June 26, 2007 · 29 comments· LAST UPDATED June 26, 2007

in , ,

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 itG+ itDownload PDF versionFound an error/typo on this page?

{ 29 comments… read them below or add one }

1 Local Service Quotes July 9, 2007 at 6:05 am

Thanks for the info and great site!


2 luca August 1, 2008 at 7:19 am

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


3 santosh February 17, 2012 at 12:15 pm

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


4 Igor November 9, 2012 at 6:58 pm

Good point – remove that slash.


5 S. Nilesh August 1, 2008 at 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.


6 S. Nilesh August 1, 2008 at 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


7 George July 4, 2009 at 2:28 pm

Nice tip, even nicer comments!


8 rebus September 12, 2009 at 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


9 guruprasad August 24, 2011 at 12:45 pm

if i do this but i have seen DNS 501 error


10 rebus September 12, 2009 at 10:59 am

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


11 John November 25, 2009 at 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


12 Anonymous May 24, 2010 at 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


13 N.A. October 18, 2011 at 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).


14 Iguano January 17, 2010 at 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!


15 Patrik Lermon January 27, 2010 at 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]


16 SomeGuy May 14, 2010 at 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.


17 M van Zwijndregt September 27, 2010 at 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]


18 Roge November 10, 2010 at 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.


19 Chris August 31, 2011 at 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.


20 rinshad December 30, 2010 at 6:37 am

this site is very good !!


21 Andreas April 13, 2011 at 2:55 pm

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)


22 Caue Rego May 2, 2011 at 4:25 am
23 bastian July 17, 2011 at 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 !!


24 Perry July 23, 2011 at 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]


25 Perry July 24, 2011 at 12:54 pm


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


26 niraj July 28, 2011 at 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]


27 Danku July 31, 2011 at 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]


28 Danku July 31, 2011 at 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?


29 Michael J May 21, 2012 at 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

Tagged as: , , , , ,

Previous Faq:

Next Faq: