Using Htaccess to redirect www to non-www

Whether you decide to use www as a subdomain or not, whats important is that you add a redirect from other option to your desired preference.

I prefer to use the non-www version, but it has no SEO benefit using either. The important thing like with the trailing slash you need to decide which you prefer and redirect it.

This can also be done in the htaccess file and once done you can forget about it.

htaccess redirect www to non-www

The below option also does the HTTP to https (I am assuming in 2019 you are using https, as there are so many positive reasons to use https and I can’t think of any real negatives.

The below is the code I use on my websites.

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

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

htaccess redirect non-www to www

Again the below code just needs adding to you htaccess file and will redirect non-www traffic to the www version of your site.

# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Its very simple code, in a future video showing you how to actually edit your htaccess file, it does require you having access to the server.

Do you have any technical questions, why not ask in our Facebook group and I will be more than happy to help.


Share on facebook
Share on twitter
Share on pinterest
Share on linkedin

Leave a Comment

Your email address will not be published. Required fields are marked *

On Key

More Great Articles

useragent lookup
Server Logs

User Agent Lookup

Have you been analysing your Server logs and realised you are unsure about the user agent and what it means.  You can try and figure

Read More »
changes to robots file

Changes to Robots File

Google has been making some big announcements in the recent week about the robots.txt file but this one if one of the biggest announcements. If

Read More »
Technical Seo Demystified - Martin Splitt
Andy Halliday

Technical Seo Demystified: Martin Splitt can’t be loaded because JavaScript is disabled: Martin Splitt – Technical SEO Demystified | AngualrUP 2019 ( This is a great talk from one

Read More »
Batchspeed Review
Technical Audits

BatchSpeed – Review

Ever been like me and wanting to bulk analyse page speeds for websites, but hated the fact its quite a manual task. Well, I’ve got

Read More »

Subscribe to my newsletter

Want to learn more about technical SEO and Server Log Analysis, sign up below and I will teach you.Each week I will send you the most helpful articles delivered straight into your inbox as well as sharing some very useful tips and tricks to improve your technical SEO skills to Ninja level.

Subscribe to our mailing list

* indicates required