Using Htaccess to redirect www to non-www

Mobile phone redirects malware concept, low key red and blue lit image and digital glitch effect

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.

Leave a Comment

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