Redirecting non-www to www with .htaccess
080227
If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This will redirect any requests to http://my-domain.com to http://www.my-domain.com. There are several benefits from doing that:
- It will avoid duplicate content in Google
- It will avoid the possibility of split page rank and/or split link popularity (inbound links).
- It's nicer, and more consistent.
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
If you want to do the opposite, the code is very similar:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^my-domain\.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]
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!
Posted in: English/Web/.htaccess
Tags: mod_rewrite, seo

Would you mind explaining exactly what each part of those two lines does? :)
Always been curious.
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. :)
Mmmh, I need to do some more work on the blog, I don't like how it automatically adds fancy quotes and converts text that looks like a url into a link.
Y si quisieras hacerlo al reves? Que todo lo que sea dense13.com vaya a www.dense13.com ?
Hi Natalia,
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.
You're right, dense13.com uses the www to non-www method, which is confusing, good point. I might reverse the order of the two methods in the post, to avoid confusion.
I kind of like reversed order also in comments, mmmh, I'll have to think about that one.
Wo-hoo! I managed to disable curly quotes and auto-links in comments! :)
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. :)
Hi, This is how my htaccess currently looks: [code] # -FrontPage-
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
order deny,allow deny from all allow from all order deny,allow deny
from all AuthName nithanth.net AuthUserFile
/home/nithanth/public_html/_vti_pvt/service.pwd RewriteEngine On
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
%{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] RewriteEngine
On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$
http://www.%{HTTP_HOST}/$1 [R=301,L] [/code] Site is nithanth.net.
Try http://nithanth.net/general/hello-world/ - it doesn't take me
to http://www.nithanth.net/general/hello-world/ , instead it takes
me to http://www.nithanth.net/index.php :(
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.
The code works perfectly, thank you.
thanks for the code, it's all that i need
Unfortunately this method messes up SSL URLs. https://my-domain.com
gets redirected to: http://www.my-domain.com Not good. :(
You could create a second .htaccess file in the folder(s) where your SSL pages are, using replacing http for https. I haven't actually tried this, but I'd say it should work.
Actually just add this line to the conditions: RewriteCond
%{SERVER_PORT} ^80$ Assuming all your http traffic is on port 80
this will do the trick.
Oh and do a separate condition/rule for port 443 (SSL)
That's neater, good thinking.
Great tip, thanks! No more putting full domain name in the
.htaccess file :)
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]
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.
Add a comment