Resolving URL Canonical Issue – htaccess

When we create a website, our website have 2 versions. First one is www version and another one is non-www. But from SEO point of view, it is recommended that we should use only one out of these. So, to achieve this, we have to redirect the another version to the preferred one.

This is the first issue. There is one more thing that we need to consider. There is one more parameter that make 2 versions of a website. First one is http and another is https. The same rule applies here also. We have to redirect the non-preferred version to the preferred one. But in this case, https version is preferred. So, if you can purchase the SSL certificate and install it. Redirect the http version to https.

Resolving various versions to https://www

RewriteEngine On
# Checks if https is set or not set
RewriteCond %{HTTPS} off
# If its not set, following line will rewrite it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, if www is not set, we will set it
# [NC] means case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You just have to copy – paste this code in your .htaccess file.

Leave a Reply

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