Use .htaccess to redirect non www to www

htaccess redirectIt’s is considered good practice, for both SEO and users, to have your website resolve to one URL. Technically speaking www.domain.com is a subdomain of domain.com. Users often type www. prior to the domain in the address bar. So to keep that formality consistent with your users you can redirect the traffic on domain.com to www.domain.com.

.htaccess

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

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

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

I have another post on how to redirect www to non-www with .htaccess.

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

Apache Cookbook: Solutions and Examples for Apache Administrators
  • afsana

    nice post

  • hola

    hola

  • jannesmannes

    Thank you for posting this simple resource.

  • Thank you for posting this resource (and being on top of the search results)

  • sgsw

    www is actually not a subdomain, it’s the name of a host at the domain.

  • Mann Sharma

    You really helped me a lot. Thank you for your posting!

  • Phil Barnes

    Just helped me sort out my issues, thanks. I had used a .htaccess generator and it failed me. Update the file to match the syntax above, good to go.

  • Thiyagu

    Thank u……

  • Chaitanya Padi

    How do you redirect from a www to non-www https domain ??

  • Useful piece of information! Thank you for sharing 🙂

  • khageswar
    • Spot Mark

      you should turn on mod rewrite

  • Manmohan

    thank you very much for solving my redirection problem of my website……….

  • BV

    Is this true for subdomains as well? If I have a subdomain called store.toyworld.com would I use the above code to redirect it http://www.store.toyworld.com? I’m assuming subdomains have their own .htaccess file and that’s were this could should be placed? Is that correct?