Redirect www to non-www with .htaccess file

It occurred to me that just maybe you don’t want to display your site with the www subdomain prefix. In that case you could deploy the following code in your .htaccess file to redirect www to non-www. By doing so, users will access your site at http://domain.com not http://www.domain.com.

.htaccess

If your website is hosted on an apache server, a redirect from www to non-www is a simple implementation. Add the following to your .htaccess file.

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

Add the above code to you .htaccess file and replace domain.com with your domain name.

This post is a follow-up to a similar adrticle on how to redirect non-www to www with .htaccess.

Want to learn more about apache? Try this resource...

Apache Cookbook: Solutions and Examples for Apache Administrators
  • http://richardwaterworth.me.uk/ Richard Waterworth

    This also worked for me on IIS

  • guo

    doesn’t work for linux

  • http://stuartmckiernan.com.au/ Stuart McKiernan

    Hi Guys,

    This should work fine on both IIS & Linux – if it’s not working I’d contact your host’s support team…

    Regards, Stuart

  • Del Syllables

    Hi this is very useful for a beginner like me. It works perfectly in Hostgator. Thanks!

  • Chris Walter

    No need to enter a domain name, this works for any.
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    • Abhisek

      Yeah this works! Works perfectly for wordpress, just have to paste the last two lines of the code by chris walter inside # BEGIN WordPress

  • TimeSheep

    Awesome, thanks!
    Might be worth noting that http:// should be changed to https:// websites, even though it’s pretty trivial :)

  • suresh

    Iam not using any redirect code for non-www to www but still it is moving to http://www..can anyone help me the problem ..!thanks in advance

    • eppand

      Can you provide a bit more info about your efforts?If you are using a CMS, it may be doing it for you.

  • eppand

    One isn’t better than the other. Just pick one and stay that path.

    • http://www.3rd.in/ Twizor Technosolutions

      without www website PR 4 and with www PR 1

      • eppand

        Technically they are separate domains / separate sites, so giving what you are seeing with PR data, I would suggest that you redirect to the without www. Most likely scenario is that already established inbound links are are pointing to your domain without www.

  • http://sehabitat.com/ Ian Mason

    This code redirects all subpages to homepage.. why?

  • cselab

    thanks for your kind information, I just need this

  • http://coolaj86.com/ AJ ONeal

    FYI, There’s also http://redirect-www.org with a growing list of instructions on how to do it for various servers and platforms (and it can do the redirection for you).

  • dsdssdf

    bvbvb