Jump to main navigation


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!

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

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

  1. #01 By natalia, 080313 at 21:03

    Would you mind explaining exactly what each part of those two lines does? :)
    Always been curious.

  2. #02 By dense13, 080313 at 22:15

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

  3. #03 By dense13, 080313 at 22:17

    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.

  4. #04 By natalia, 080313 at 23:43

    Y si quisieras hacerlo al reves? Que todo lo que sea dense13.com vaya a www.dense13.com ?

  5. #05 By dense13, 080314 at 17:58

    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.

  6. #06 By natalia, 080314 at 19:26

    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.

  7. #07 By dense13, 080315 at 07:57

    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.

  8. #08 By dense13, 080329 at 00:26

    Wo-hoo! I managed to disable curly quotes and auto-links in comments! :)

  9. #09 By Nithanth, 081001 at 05:25

    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

  10. #10 By dense13, 081001 at 09:36

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

  11. #11 By Nithanth, 081002 at 01:23

    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 :(

  12. #12 By dense13, 081002 at 09:37

    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.

  13. #13 By Alecu, 090521 at 20:10

    The code works perfectly, thank you.

  14. #14 By quantro, 090821 at 02:56

    thanks for the code, it's all that i need

  15. #15 By John M, 100126 at 07:19

    Unfortunately this method messes up SSL URLs. https://my-domain.com
    gets redirected to: http://www.my-domain.com Not good. :(

  16. #16 By dense13, 100126 at 08:08

    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.

  17. #17 By John M, 100126 at 08:14

    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.

  18. #18 By John M, 100126 at 08:14

    Oh and do a separate condition/rule for port 443 (SSL)

  19. #19 By dense13, 100126 at 08:23

    That's neater, good thinking.

  20. #20 By Katalog, 100619 at 01:10

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

  21. #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. #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. #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?

  24. #24 By Asad Kay, 100917 at 22:51

    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?

  25. #25 By dense13, 100918 at 12:09

    @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. #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?

  27. #27 By dense13, 101115 at 09:08

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

  28. #28 By vijay, 101213 at 20:26

    Really nice explanation for redirect condition.

  29. #29 By phil.t, 110117 at 20:14

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

  30. #30 By Peter, 110202 at 23:32

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

  31. #31 By westenddogwalking, 110202 at 23:35

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

  32. #32 By foci eredmények, 110211 at 02:42

    Thank you for this great post.Finally found what i need to redirect
    all of my pages non www to www.Really helpful!

  33. #33 By Champs, 110211 at 23:12

    Thanks for help!!!!!!!!!!!!!!

  34. #34 By Just How Important Are Domains for Keyword Ranking? | Free Articles Directory, Article Submission - IMFreakz.com, 110223 at 12:08

    [...] For non-www to www [...]

  35. #35 By Laurent, 110317 at 09:29

    Hi, What is better for SEO www to non-www or non-www to www? I have
    read that one way if good for SEO but the other not. What is the
    best way? Thanks

  36. #36 By dense13, 110317 at 09:48

    @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. #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. #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.

  39. #39 By picsel, 110516 at 07:07

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

  41. #41 By faheem, 110605 at 05:59

    very nice solution through .htacess. I was looking for some plugin
    for to do the job of shifting www to non www

  42. #42 By WpRack, 110623 at 08:19

    Very helpful... I was able to redirect my main blog but unable to
    redirect the addon domain to www version.

  43. #43 By Rediriger une adresse non-www vers www avec .htaccess | Geek-Lab, 110712 at 19:46

    [...] pour n’importe quel site, puisque le nom de domaine n’est pas
    dedans.Via : Redirecting non-www to www with .htaccess Image via :
    DatacenterArticles RelatifsFaire détecter son Eee par sa Freebox3
    commandes à ne pas [...]

  44. #44 By Sn3ak, 110802 at 10:42

    Thank you! Someone gave me the opposite code! >:(

  45. #45 By MiC, 110825 at 03:50

    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?

  46. #46 By dense13, 110825 at 10:37

    @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. #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

  48. #48 By dense13, 110827 at 12:15

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

  49. #49 By MartynG, 110909 at 20:15

    Hi My rule for redirecting making flat links works, but how do i
    combine the two rules please?? RewriteEngine on RewriteRule
    ^page/([a-z]+) home.php?page=$1 [NC] RewriteCond %{HTTP_HOST}
    ^domain\.co\.uk [nc] RewriteRule (.*) http://www.domain.co.uk/$1
    [R=301,L]

  50. #50 By dense13, 110909 at 23:25

    @MartynG: first do the www redirection, then send everything to home.php.

  51. #51 By cyndi, 110915 at 23:46

    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

  52. #52 By dense13, 110916 at 08:48

    @cyndi: I don't think so, but I don't know much about IIS.

  53. #53 By heru, 111005 at 07:05

    can you help me to resolve the error at
    http://www.kerjatop.com/sitemap.xml otherwise but there is no error
    with http://kerjatop.com/sitemap.xml

  54. #54 By dense13, 111005 at 11:49

    @heru: what redirection are you using? What are you trying to do exactly?

  55. #55 By ru-charter, 111012 at 08:19

    Excelent it works to me, i was lookin' all over internet, and i
    finally get! It's a got idea to redirect to only one page!

  56. #56 By KLynne, 111016 at 05:20

    Thanks so much! What a life-saver.

  57. #57 By Cardiff IT Support, 111018 at 06:45

    Thanks so much, looking for this for ages :D

  58. #58 By Claude, 111020 at 06:57

    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?

  59. #59 By Irham, 111025 at 16:44

    How to add the code in the wordpress installation? my site got
    error when adding the code. # BEGIN WordPress RewriteEngine On
    RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond
    %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L] # END WordPress

  60. #60 By Autoverzekering, 111027 at 05:37

    Perfect formula, since it's generic!

  61. #61 By Web dizajn, 111211 at 11:58

    Working for me! Thank you very much!

  62. #62 By imade, 111217 at 17:50

    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?

  63. #63 By Pakaian Kerja Wanita, 111227 at 13:51

    I've tried this method but i was still confused use this in another
    directory..

  64. #64 By Vernon, 111229 at 01:11

    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

  65. #65 By Raptorak, 120102 at 11:33

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

  67. #67 By sajee, 120123 at 17:19

    thanks brooo

  68. #68 By Tanveer, 120129 at 03:18

    I'm having issue while trying to redirect non www to www in
    wordpress. it gives redirect loop errror :(

  69. #69 By Fadi, 120131 at 03:56

    It would be great if you can also create an IIS version for non-www
    to www redirection. Not everyone uses Apache as a web server.

  70. #70 By Rob, 120210 at 09:46

    when I do this, all I ever get is an endless browser redirect
    warning (i'm using WordPress also): I have this: #non-www to www
    start RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule
    ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] #non-www to www end #
    BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule
    ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
    %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END
    WordPress any ideas??

  71. #71 By renuka, 120216 at 05:01

    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 ?

  72. #72 By dense13, 120220 at 08:18

    Sorry renuka, I can't help with that. I'd go to a SEO-specific forum and ask there. I hope you can sort that out quickly.

  73. #73 By Alex, 120225 at 03:05

    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 changed
    RewriteCond %{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.

  74. #74 By milos, 120302 at 05:56

    very nice solution through .htacess.

  75. #75 By Sarah, 120302 at 13:05

    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.

  76. #76 By dense13, 120302 at 14:20

    Sorry about all the people asking for help, I'd like to help out but I don't have much time available now. I will try to find some, but can't guarantee...

  77. #77 By Ronan, 120326 at 10:25

    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?

  78. #78 By my site, 120406 at 11:03

    Does it matter what kind of software you are running in order to
    this. If it is a wordpress site, don't they have a plugin for
    something like this?

  79. #79 By Soccerwidow, 120415 at 20:01

    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.

  80. #80 By Photobooth Manila, 120503 at 13:44

    Thank you! This just helped my website to avoid duplicate and
    splitting the rank.

  81. #81 By bokep online, 120509 at 19:51

    Nice post... I really need for my web... Thanks for your share.

  82. #82 By san han quoc, 120514 at 17:25

    i added this code: RewriteEngine On RewriteCond %{HTTP_HOST}
    !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] on
    file .htacess. but it's not work.

  83. #83 By desklancer, 120520 at 06:07

    @Ronan it seems you put your .htaccess in wrong location read
    comment no 21 and 22 put your .htaccess file in correct directory

  84. #84 By maddy, 120522 at 22:13

    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

  85. #85 By sab.fm, 120525 at 04:39

    Hi admin How to redirect entire website to home page i tried many
    ways but i didn't succeed can you please help on this

  86. #86 By Nikolay, 120608 at 16:08

    Thanks for htaccess :) It saved me 5 minutes (:

  87. #87 By Muslim Tradisional, 120615 at 14:03

    Ha, thank you very much for the tutorial. I've used this similar
    htaccess redirection technique several times, it's so useful :)

  88. #88 By rahul, 120616 at 16:44

    how can we redirect domain1.com to domain2.com ? i have own both
    domain but don't know how to redirect.

  89. #89 By US Joomla Force, 120620 at 19:53

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

  91. #91 By dense13, 120712 at 09:05

    @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. #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

  93. #93 By dense13, 120712 at 16:09

    I'm sorry about the poor formatting of code in the comments, I really should fix that.

  94. #94 By Jasin, 120713 at 19:16

    Hi, Great!!! It works... It is redirecting as I want. But I
    couldn't login to my admin and members also can't login with their
    old login details. How to fix it? Thanks, Jasin A.

  95. #95 By Brad, 120714 at 23:52

    Thank you.

  96. #96 By Abnehmen, 120728 at 05:07

    The code perfectly worked on my website. I believe redirecting
    non-www to www is a great way to not loosing pagerank

  97. #97 By Socieal Bookmarking Site, 120728 at 07:43

    Thanks lot i am thankful to you. you helped me lot. your
    information is worth to know. i am able to redirect the url after 3
    day.

  98. #98 By Mike, 120803 at 19:00

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

  99. #99 By Mike, 120803 at 19:01

    Apologies. That has just started working. There must be a time
    delay between loading the file and server recognising the htaccess!
    Thanks for your post anyway

  100. #100 By CrossFit Nutrition, 120906 at 21:27

    Excellent. I have been trying for about an hour to find the correct
    code to do this and yours is the first successful method with by
    far the neatest solution too. Thank you

  101. #101 By eBlix, 120911 at 06:29

    Thanks for the Clear info. For those who changing .htaccess in
    Opencart application : Dont forget to change your site url to www.
    in config.php in root folder and config.php /admin/ folder.

  102. #102 By alexander, 120913 at 01:52

    could you look into this and tell me my mistake?

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} !^$
    RewriteRule ^/?(.*) http://www.%{HTTP_HOST}/$1 [R,NE]
    RewriteCond %{HTTP_HOST} ^(www.)?mywebsite.(com|co\.uk)$
    RewriteCond %{REQUEST_URI} !^/www/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /www/$1
    RewriteCond %{HTTP_HOST} ^(www.)?mywebsite.(com|co\.uk)$
    RewriteRule ^(/)?$ www/index.html [L]

    thank you!

  103. #103 By ambest, 120920 at 00:12

    good working....thank you

  104. #104 By waroengpakaian, 120920 at 00:34

    thanks alot brother,,,,

  105. #105 By Spark, 121009 at 23:38

    It works, great, thanks! Now I am struggling with a file extension
    modifier code... hmm won't work!

  106. #106 By Naveen, 121029 at 03:22

    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.

  107. #107 By dense13, 121029 at 11:14

    @Naveen: since that's a different issue, I wrote a whole post about it. :)
    Removing index.html with mod_rewrite in .htaccess

  108. #108 By ShadyCraig, 121031 at 22:20

    Thanks,

    This helped a lot!

  109. #109 By Dominic, 121103 at 19:32

    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 your time and an extremely useful post,

    Dominic

  110. #110 By Campus Ng, 121105 at 03:05

    The www to non www htacess redirect is not working for me. I don't know that reason for that :(

  111. #111 By Bollywood, 121119 at 22:20

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

  112. #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. #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. #114 By Bep, 121217 at 05:55

    Thank you very much.

  115. #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. #116 By Andreas Pazer, 130105 at 06:52

    The code works perfectly, thank you.

  117. #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. #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

  119. #119 By wade, 130115 at 06:14

    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?

    here is original htaccess working code:(just from the tag)

    RewriteEngine On

    RewriteBase /mediasolv/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /mediasolv/index.php [L]

    In order to get non www to direct to www using your fix I changed to:

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

    Brought down the site.

  120. #120 By helenna, 130116 at 14:49

    what haven if i do not redirect non www to www? flagged as duplicate content by Google?

  121. #121 By Bibin varghese, 130121 at 05:22

    I am a newbie on wordpress
    Where i put the above code on htaccess( which line)???

  122. #122 By Jens, 130126 at 01:10

    Works!!!! Thanks!

  123. #123 By Bangkok Hotels, 130202 at 18:03

    Great thanks for the info. Great that both redirects are listed.

    Also good to add if people are getting crawl errors for old pages that they have renamed and have backlinks to:

    RewriteRule ^about_us.php$ "http://www.yourdomain/about_us.htm" [R=301,L]

    Which will redirect the old "about us" page to the new page.

  124. #124 By Aussie, 130207 at 15:05

    When I use this code on a WordPress site, I put it directly above the wordpress info in htacces and it works perfectly

  125. #125 By DanielS, 130221 at 16:14

    Work's just as advertised, and was very well explained in the comments very early on. Thanks for this post, it's still very helpful!

  126. #126 By Jens, 130227 at 02:17

    Is there also another way?

  127. #127 By Gabi Radu, 130227 at 04:30

    Dense. I need www to non www version of all urls. Please provide to us a version for this too.. I know that it must be easy.. but i dont want to broke anything.. i'm new to this.

    This should be ok ?

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

  128. #128 By Sarah, 130302 at 21:26

    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

    RewriteCond %{THE_REQUEST} ^.*/index.html
    RewriteRule ^(.*)index.html$ http://www.url.com.au/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^url.com.au$
    RewriteRule (.*) http://www.url.com.au/$1 [R=301,L]

  129. #129 By ?????, 130309 at 00:23

    can we use php redirect instead of htaccess way?

  130. #130 By Pradeep Sharma, 130309 at 16:50

    Urgent:
    why my home is redirecting to my-domain.com even after i did correction
    replace this code
    ----------
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^my-domain.com$ [NC]
    RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]
    --------------
    with
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^shoppingmart.comyr.com$ [NC]
    RewriteRule ^(.*)$ http://shoppingmart.comyr.com/$1 [R=301,L]

    after that, default, my-domain.com is opening....why, tell me urgently

  131. #131 By Henley, 130407 at 12:49

    So I did this and it did change it to a WWW host but now it does
    this: http://www.www.mysticania.x10.mx/

  132. #132 By Amiya, 130416 at 04:47

    Thanks, After setting non-www to www, i don't know why but site
    loading more faster. I like first code because it has no site
    related issue i mean site url. Tanks again..

  133. #133 By Chris, 130417 at 13:40

    Thanks for the article, much appreciated. Definitely don't want
    duplicate content on Google.

  134. #134 By Gesund Abnehmen, 130417 at 17:30

    Thank you. Have just set up a htaccess and followed all to the
    www.site...it workes great thank you!!

  135. #135 By fireball, 130525 at 22:50

    Thank you! It works like a charm!

  136. #136 By Renee Skolnick, 130603 at 20:57

    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.

  137. #137 By approved electrician cardiff, 130624 at 11:09

    great info, i was stuck on this for ages, many thanks, regards
    chris

  138. #138 By Microjobs, 130805 at 01:59

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

  139. #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. #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. #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.

  142. #142 By top ciga, 130829 at 21:01

    Nice, thanks for this htacess tricks, usefull. Prevent from
    duplicate content.

  143. #143 By serjuc, 130904 at 21:12

    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. #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. #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. #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. #147 By Kannan, 131113 at 13:13

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

  148. #148 By sina, 131223 at 00:43

    Now I'm getting this error ERR_TOO_MANY_REDIRECTS

  149. #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

Add a comment


Allowed XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <p> <strike> <strong>


To prove you're a person (not a spam script), type the answer to the math equation shown in the picture. Click on the picture to hear an audio file of the equation.
Click to hear an audio file of the anti-spam equation =

Additional content and navigation

Categories

Main navigation menu