Note that if your site has already been indexed by Google without the www, this might cause unwanted side effects, like lost of PR. I don't think this would happen, or in any case it would be a temporary issue (we are doing a permanent redirect, 301, so Google should transfer all rankings to the www version). But anyway, use at your own risk!

Something nice about the code above is that you can use it for any website, since it doesn't include the actual domain name.

Redirecting www to non-www

In this case we are explicitly typing the domain name. I'm sure it's possible to do it in a generic way, but I haven't had the time to work one out and test it. So remember to change 'my-domain' with your domain name!

158 comments to “Redirecting non-www to www with .htaccess”

Sure! The first line sets a condition: only if the condition is true, the second line will be processed. The condition could be 'translated' to: "if the host name doesn't start with www.". The regular expression !^www\. means this:

! = not
^ = start
\. = . (the backslash is the escape character, because dots have a special meaning in regular expressions, and therefore must be escaped)

So !^www\. means "doesn't start with www.".

The second line is the actual rewrite rule: again it uses regular expressions to match certain urls, and then rewrites them to something else. The first part is the regular expression:

^(.*)$

This means: anything! You already know the ^ sign. The (.*) bit means zero or more characters (the dot means any character, the asterisk means zero or more). The final $ means 'end'.

Then comes the bit that says how to rewrite the url:

http://www.%{HTTP_HOST}/$1 [R=301,L]

%{HTTP_HOST} will be replaced by the host name (i.e. dense13.com).
$1 references whatever was matched in the regular expression between the brackets, which in this case is everything.
The [R=301,L] means "inform the user agent that this is a permanent redirect (HTTP 301 code), and don't process any more rewrite rules (if there were any after this one).

If you're not familiar with regular expressions, this might still look a bit abstract, feel free to ask for more details. :)

I assume you mean how to redirect from the www version to the non-www version. I've updated the post to include that option as well.

If you're curious, the only thing that's new in that code is the [NC], which stands for no-case (I think). That means that the pattern before will be case-insensitive, and thus will match my-domain.com, My-Domain.com, mY-dOMaiN.CoM, etc.

So I got confused because I types www.dense13.com and it sent me to dense13.com so I assumed you were explaining how to do that www to non-www even though the title says the opposite ... yep, I know how that sounds, don't judge :P
That is also why the actual explanation didn't seem to make a lot of sense :lol:

I actually had to change a .htaccess file without having a clue about the whole thing and somehow made it work, I seem to have some inner intuition that lets me do things my brain doesn't quite grasp.

Minor thing...Comments in reverse order are kind confusing, with posts it works cos one doesn't follow from the previous one, or if it does they are normally linked, comments on the other hand...especially long ones...one has to keep scrolling up and down to follow the conversation if you know what I mean.

Hi, Great tutorial, thanks. Also, thanks for taking time to explain
what exactly each of those symbols meant :) One question. After
using the non-www to www redirect, it redirects my site to the www
version just fine. But the thing is, if I enter in
site.com/somepage.html , it redirects me to www.site.com and not
www.site.com/somepage.html. Is there any way to make it do the
latter? Thanks in advance :) Nithanth

Hi Nithanth. That's strange, it should be redirecting you to the right page. First thing would be to make sure you typed the right code in your .htaccess, which you've probably done already. Feel free to paste your htaccess code here or mail it to me at gperezdelaossa_AT_dense13_DOT_com . I don't know if I'll be able to help, but I can try. :)

Mmh, I'll have to offer a way for people to post code, it's all messed up, sorry! I'll try to re-format it, and have a look. I might not be able to do it straight away though, I'm going through a bit of a busy period right now, but will let you know when I do.

Great tip, thanks! No more putting full domain name in the
.htaccess file :)

#21 By Francis Mariani, 100720 at 09:47

Thanks very much for this post. I created a .htaccess file with
your code copy-pasted for website www.limlum.com. When I type
limlum.com in the web browser address, it redirects to
http://www.limlum.com/public_html/ which does not exist and I get a
404 error. Am I missing another file on the webste?RewriteEngine On RewriteCond %{HTTP_HOST} !^www\\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#22 By Francis Mariani, 100720 at 10:14

Sorry I posted before testing properly - I put the .htaccess file
in a directory one level above public_html which is the wrong
location. I put it in public_html and it now works. Thanks.

#23 By Asad Kay, 100917 at 22:45

Bro, what about having subdomains? I used your code but now this
code is forwarding my subdomain to a www site as well. So how do
you add an exception to the code for a subdomain?

Nice code. Works fine but what about if someone is running
subdomains? The code forwards my forum sub domain to www.forum. ...
etc. So how can I add an exception to the code not to touch the sub
domains?

@Asak Kay: just change the Rewrite condition to include any other subdomains besides www:

RewriteCond %{HTTP_HOST} !^(www|sub1|sub2)\.

That should do it.

#26 By nyha, 101114 at 07:18

I's supposed you will need to write RewriteRule ^(.*)$
http://www\.%{HTTP_HOST}/$1 [R=301,L,NC] instead of RewriteRule
^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] I'm add /. and NC, nc
here for no case option, so you can write your domain in any case
Am I correct?

@nyha: I dont think you need to escape the period in the second part of the RewriteRule, that's not a regexp. And not sure about the NC, I think that's to use in the condition (as in 'Redirecting www to non-www' in the post). But might be worth testing, I'll see if I can do it some time soon.

Thanks for this post. Having just changed webhosts and the new one
not offering a GUI for updating the www to non-www redirect and
thanks to GOOG I've landed here and am now learning the basics of
.htaccess editing (secretly, it tickles the geek in me :) Thanks,
also, for explaining the entry. As easy as it is to just cut and
paste something I, personally, prefer to understand how to do it.
Although I think I'll have to re-read it a few more times first.
Once again, cheers. :)

Thank you so much for this post !! I had the same problem as Asad
Kay (in comment nr.24) with the sub domains and your solution (in
comment nr.25) was a life saver for me !! It's very simple when you
think about it, but I didn't and was struggling with it for half a
day !! Thanks again !!

Great post, thanks a lot!! I had an error in my code and could not
figure out why I got an interal server error until I found this
post. Just copied the code and pasted into my htaccess and it works
perfectly. Again, thanks for the post :)

@Laurent: I don't know, you'll have to look for some SEO specific resource. I suspect it won't make any difference, but that's just my intuitive opinion.

#37 By Andreas, 110414 at 02:38

Hi, I would like to use mod rewrite to redirect domain-N.tld to
www.domain-N.tld (1.) sub-M.domain-N.tld to sub-M.domain-N.tld
(keep the original, don't add www). (~ if the request is domain.tld
-> rewrite, else keep) I have over 100 domains, so N,M>100,
and I need regexp. (1.) works fine, but I can't remove the www from
the subdomains. If you can, please help me. Thx.

#38 By Jack, 110515 at 03:35

Thanks so much for the right code for non-www to www. I have been
trying for a week to get my site to redirect. I have searched many
sites for advice, but unfortunately nothing worked until I put your
code in. I added [NC] after "RewriteCond %{HTTP_HOST} !^www\.
[NC]". Works great! Thanks again.

Hi, I would like to use mod rewrite to redirect
mysite.com/index.php to www.mysite.com ,but I can't ! If you can,
please help me. Thank's...

#40 By Sally, 110520 at 06:19

Hello, thanks for your excellent site. Perhaps you can help with my
problem. Any help is appreciated! I want to redirect my /index.html
to my "/" (www.sample.com). There is only one domain on the server.
I am using the following script: RewriteEngine On RewriteCond
%{THE_REQUEST} /index\.html\ HTTP/ RewriteRule ^index\.html$ /
[R=301,L] It works, but if you type other things in your browser,
it brings up a webpage but without all the shapes/pics/etc. For
example: www.sample.com/paper/index.html would bring up the webpage
www.sample.com/paper with the text, but without the shapes, pics,
etc. I want the .htaccess to redirect those types of requests to
the www.sample.com/ page (and of course, without the .index.) I
tried: RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\
/(([^/]+/)*)index\.html\ HTTP/ RewriteRule index\.html$
http://www.example.com/%1 [R=301,L] It works about the same as the
first example.

Hi there, What code do i need to program to redirect ONLY
http://mydomain.com to http://www.mydomain.com i tested the first
one in this topic and it went good but it went wrong when i
navigate to my ssl url https://secure.mydomain.com can anyone help
me with this?

@MiC: check out comments 15 to 17. Try what John M suggests in 17, I haven't tested it myself but it should work.

#47 By Anand, 110826 at 21:00

Hi, I got a queriy and wanted your help in this regard. When people
access my domain it is redirected to
http://www.mydomain.com/en/index.php file using php code.I used
RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$
http://www.%{HTTP_HOST}/$1 [R=301,L] to redirect people from non
www to www but its not working. Still user can access
http://mydomain.com/en/index.php

@Anand: that code should work, there might be other parts of your .htaccess file influencing it (assuming you are using an .htaccess file). I suggest commenting things out and make sure the redirection is working correctly, and then bring things back step by step, to locate the problem. You might also want to post to a forum, including your .htaccess code (I'm keen to help when I can, but have limited time).

hi dense13, can you tell me if your code to redirect(non-www to
www) will work on an iis6 server without using any kind of plugins
or other installations (ie. heliotech - isapi rewrite)? our pages
are coming from an apache server to the iis6 server. thank you

Question concerning non-www to www. I have a site in which a
discussion forum resides in one of the sub-directories. The
standard rewrite plays havoc with the Forum but I still want to use
the redirect on the root directory only. Is it possible rewrite
non-www to www for the root directory only? Exclude rewrites for
sub-directories?

Thanks for nice post. I've implemented this on my web. It redirect
from mydomain.com to www.mydomain.com. but when I try to open
directly from URL: mydomain.com/somedirectory/somepage.htm it still
not redirected to www.mydomain.com/somedirectory/somepage.htm Can
you help me how it could redirected as well?

Hi this is my .htaccess file in joomla and sends people to my new
site and it also redirects www to non www it works fine but how do
I make a permanent (301) Redirect RewriteEngine on RewriteCond
%{REQUEST_FILENAME} !-f RewriteRule ^(.*)index.shtml$
http://www.mynewsite.com/index.php RewriteRule ^/(.*)$
http://www.mynewsite.com/index.php

Hey, just wanted to say thanks! After reading over many tutorials
on how to go about this I wasn't fully understanding the method
(many kept trying to input URL's as well and I needed a generic
method). This worked flawlessly, :).

#66 By Gowranga Chintapatra, 120118 at 23:38

Hello, Thanks for your tutorial. It's working fine. But I need my
site to be redirected to ROOT for all version of index.php. For
example if I type http://www.grantmarketinggroup.com/index.php it
will be redirected to http://www.grantmarketinggroup.com/ . I have
tried lots of rewrite rules but no results. Hope you will find time
to respond me. Thanks again. Gowranga

after creating .htaccess with above information to convert www to
non www my site traffic decreased to 50% in 1 day and in webmaster
page index its showing pending. i have undo the changes still i
cannot see any change in traffic its going on decrease..any idea
again when my results will be normal in google ?

Thank you so very much for this information and thorough
explanation on how to redirect www to non-www and vice-versa with
htacess. I don't have subdomains so far, but I thought I'd
implement the line on Comment #25 just in case. But when I changedRewriteCond %{HTTP_HOST} !^www\. into RewriteCond
%{HTTP_HOST} !^(www|sub1|sub2)\., the redirecting stopped.
Redirecting resumed as soon as I switched back to the original
line. Any ideas why? Should I have added the line in Comment #25
instead of substituting? Thanks.

Wondering if it's possible to redirect a website folder link that
comes before another one - so, redirecting without effecting any
further sub folders links? For instance, I have a page called
"www.xxx.com/abc/1" But I don't use /abc/ folder anymore and want
to redirect it to /def/ folder instead. So, www.xxx.com/abc/
redirects to www.to xxx.com/def/ But when I create a .htaccess with
the codes below; URL PERMANENT REDIRECT Redirect permanent /abc
http://www.xxx.com/def Then, abc/1/ folder also wants to redirect
to /def/1/ Since there is no page called def/1/ on my server and no
chance to create/move the page due to some complexities, how do you
think I can resolve this issue with .htaccess? Thanks.

Great post... thanks for sharing! I am having a problem with this
and was hoping you could give me a steer in the right direction.
When I upload the .htaccess file and then enter in the url without
the URL e.g website.co.nz the browser is redirecting to
www.website.co.nz/www/htdocs/ I cannot work out where it is picking
up the /www/htdocs element from. Can you shed any light?

Thank you, great code! But unfortunately, the code doesn't work for
me. I'm having a multilingual blog with different domains for each
language. The settings are that the second language is a server
alias (mirror of the main domain). The redirecting works "kind of"
but brings the content in one language and sidebar/header in the
other language.

Hello webmaster i need some help regarding the non www to www
redirection. I placed the below code in the domain.com but its not
redirecting RewriteEngine on rewritecond %{http_host} ^domain.com
[nc] rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc] Please
suggest me regarding this

Hi Rahu, Use the following code to redirect from one domain to
another : RewriteEngine on RewriteCond %{HTTP_HOST}
^www.old-domain.com$ [NC] RewriteRule ^(.*)$
http://www.new-domain.com/$1 [R=301,L]

#90 By Daniel, 120712 at 07:44

One thing I don't get is the backreference. If (.*) matches the
entire URL, then shouldn't the resulting URL after the rewrite look
like: http://www.somesite.net/http://somesite.net?? According to
official Appache documentation the pattern is tested against the
whole URL, but it behaves as if it is only testing the URI.

@Daniel, from the mod_rewrite docs:
"In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html")."

#92 By Gopalb123, 120712 at 13:15

Hi' I hv problem with www and non www. my htaccess is below:# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule
^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END
WordPress without www is ok. but withh www.domain name.com
not working... what sould i do for both working (www and non-www) I
read your article but I dont understand .... please help me

Nice post - very clearly explained. I want to rewrite
http://thecoffeelocator.com to http://www.thecoffeelocator.com and
have used the following: RewriteEngine On RewriteCond %{HTTP_HOST}
^http://thecoffeelocator\.com [NC] RewriteRule ^(.*)$
http://www\.thecoffeelocator.com/$1 [L,R=301] But this is not
working. Do you have any ideas? Thanks
"http://www.thecoffeelocator.com" title=
"Coffee Locator>The Coffee Locator</a></div></body></html>">

I have used your code above to apply redirections from non-www to www. However, how to redirect http://www.example.com/index.html to http://www.example.com. This question was also asked by Gowranga Chintapatra in comment 66.

One problem with our caching in Joomla is that it's caching pages as anything anyone inputs to get to the site, so that if some intputs blahblah.domain.com/whatever people get served that URL next time they visit.

Your fix for non www to www works to an extent, but in this instance what we get is www.blahblah.domain.com.

Plus if people use a re-direct, let's say domain.es, this gets served too.

I've tried searching everywhere and can't come up with a fix - so is there something that can re-direct and blahblah or TLD to good old domain.com/whatever.

Thanks for the post. I am looking to redirect ip address to domain. How can we do that?

#112 By EWebGenie, 121121 at 19:25

this is very good. even now a lot of admins dont know the the htaccess redirection. htaccess is very helpful in seo aspect, which removes the site and content duplication by redirecting the non www to www versions and index to root , custom error page and many more useful redirections.. Thanks for giving the htaccess code

#113 By bill, 121209 at 04:58

How would you recommend removing the /index.html part from the http://www.host.com/index.html url.

#114 By Bep, 121217 at 05:55

Thank you very much.

#115 By Craig, 121220 at 08:36

Just wanted to add that I modified the rule so that it isn't specific to protocol for the redirect:RewriteRule ^(.*)$ %{SERVER_PROTOCOL}://www.%{HTTP_HOST}/$1 [R=301,L]

#116 By Andreas Pazer, 130105 at 06:52

The code works perfectly, thank you.

#117 By john, 130111 at 06:10

Hi, I'm not able to redirect the whole site to www version. The main page redirects perfectly ( example.com to www.example.com)
But if I try another page or directory, it does not redirect, and displays the same non-www url in the address bar(example.com/services DOESN'T redirect to www.example.com/services, which I need) . Please help me I have tried everycombination but it doesnt seem to work :(

#118 By john, 130112 at 20:24

Ok I played with htaccess a little and now i am able to redirect the subpages without www to the homepage with www, but now stuck here, tried everything..
for exampl, example.com/services redirects to www.example.com/index.php :S Please help

i am not able to get the non-www to redirect to www.
i aplied the changes above to do so and got a server error taking my www.mediasolvcorp.com site down.
not sure what i did wrong, can u check my code changes and see if you can tell where i may have screwed up?

I have a .htaccess redirect file to convert all variations of my url to be just the one, eg, to change eg, url/index.html, www.url.com.au, etc.

However it seems to change them all to no http or www. Now I want to install wordpress on a subdomain blog, it wont work because of the redirects. WordPress requires a http://www.url.com.au to work. Any idea if I can adjust the.htaccess code a little to redirect all variations to have a http://www.url.com.au

1 down vote I ran this... RewriteEngine on RewriteCond %{HTTP_HOST}
!^www.*$ [NC] RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/$1
[R=301,L] I need this to be universal for 25+ domains on our new
server, so this directive is in my virtual.conf file in a tag. (dir
is parent to all docroots) I had to do a bit of a hack on the
rewrite rule though, as the full docroot was being carried through
on the pattern match, despite what
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html says about it
only being stuff after the host and port.

thank you very much for your article, plz i need how to redirect
all site to https? i wait your repley Regards

#139 By junior, 130815 at 20:35

Hi, My wordpress website was hacked, and when I access it without
www, it brings to a hacked page. So I've thought adding this line
of code to .htaccess but there are some lines of code put by
wordpress. Should I delete them and use your code? WORDPRESS CODE:
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule
^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END
WordPress --------------------------------------------------- YOUR
CODE: RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule
^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

#140 By Edward, 130818 at 23:05

Thank you a lot for this! It really should be the first thing one
finds if one Googles for redirecting a naked domain to www. I had
some odd issues with cookie management when users came from the
naked domain that I really didn't want to debug. So now I don't
have to. And that is in addition to the perks you already mentioned
in your article. AND I don't have to pay for domain forwarding. So
great! :)

#141 By Rick, 130819 at 02:37

Great post (and comments, especially the one explaining the regex).
I was looking for a way to redirect visitors who typed in the ip of
the server instead of my domain name. Example:
http://123.123.123.123 would also display my site and I wanted to
make sure those visitors were redirected to
http://www.mydomain.info instead. Thanks to you I managed to figure
it out. Thank you.

hy everyone! i use this to redirect all www pages to non-www
RewriteEngine On RewriteCond %{HTTP_HOST} !^my-domain.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L] but...i have
subdomains activated on my server if someone type
www.subdomain.domain.com redirect to domain.com i want to redirect
to subdomain.domain.com have a solution for me? thank you!

#144 By phân bón, 130918 at 17:45

I buid phanhuucovisinh.vn, it default non-www, i want www, but i
don't see .htacess

#145 By Nike Novillo, 130925 at 23:32

Hi there! I have my site in blogger. If I redirect, all the links
to files in my server break... Is there a way to include an
exception for specific folders like domain.com/images/picture.jpg
so they are not redirected? Thank you!

#146 By Abhishek, 131020 at 14:59

Excellent, in just 15mins I am able to understand & integrate
this. THank you for the quick & simplest solution you are
giving.

#147 By Kannan, 131113 at 13:13

Thank you for the article and explanation. It helps to understand
rewrite rule in detail.

#148 By sina, 131223 at 00:43

Now I'm getting this error ERR_TOO_MANY_REDIRECTS

#149 By Nige, 140101 at 00:24

If your site is set to force https RewriteRule ^(.*)$
https://www.%{HTTP_HOST}/$1 [R=301,L] before the http to https
rules

Hello i am using Amazon EC2 as my server and PHP Dolphin as a
social networking script and i have used this code this code
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule
^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] but its not working on
this so kindly suggest me how to solve this problem

FYI: You can also use http://redirect-www.org to handle the
redirect for you if you have access to DNS but not .htaccess (or
are just having trouble with it).

#152 By siva, 140617 at 22:19

Hi , I have this rewrite engine for converting http requests to
https.When i type it converts to https://www.domain.in but when i
type https:domain.in it is not rediected to https://www.domain.in
How i need to get this worked RewriteEngine On RewriteCond %{HTTPS}
off RewriteRule ^(.*)$ https://domain$1 RewriteRule (.*)
https://%{HTTP_HOST}%{REQUEST_URI}

I want to redirect to https://www.website.com/ I did the following
combo. RewriteCond %{HTTP_HOST} !^www. RewriteRule ^(.*)$
https://www.%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{SERVER_PORT}
!^443$ RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
[R=301,L] It works, but is it correct?

#156 By Karen Nicholson, 141213 at 20:15

I wanted to thank you for your explanation of this .htaccess 301
redirect as although my wordpress setup was using the www directive
in the site url the domain level incoming links were still being
attributed to the non www version. All wordpress users need to be
aware that prior domain owners may have sent links to the non www
domain and you should use the information in this article to make
sure you can credit that old link juice back to your www domain.

#157 By Aptoide, 141223 at 04:58

Hello Thanks for the guide! how I can move multiple url, s from one
domain to another? example: www.example.com to www.example.es -
www.example.com/contact.php to www.example.es/contect.php Thanks!!

#158 By Blauwe, 150207 at 08:01

One or two people might be interested in this: Remove "www" from
any URL without affecting it's HTTP or HTTPS from the org. request.
(I needed to remove www while leaving intact that some pages on the
site should open in http, others in httpS. RewriteCond %{HTTP_HOST}
^www\. RewriteCond %{HTTPS}s ^on(s)|off RewriteCond
http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$ RewriteRule ^
%1%3%{REQUEST_URI} [R=301,L] Tested and works wonderful. - Blauwe