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:

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn

Leave a Comment

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

On Key

More Great Articles

What is Gutenburg?
Server Log Kid
Andy

What is Gutenburg?

Gutenburg is coming soon, it will be the default wordpress editor for writing blog posts and pages.

Read More »
Speed kills your business
Speed kills
Andy

Speed kills your business

Speed kills your business. A slow loading website is going to increase the bounce rate meaning you’re going to have to get a lower RIO

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.

* indicates required