Submitted by admin@soz on Fri, 01/17/2020 - 21:41

If we have a redundant hostname, Google Analytics will tracking our domain www.example.com and example.com independently. This means that our pages will be split across two URLs even if they provide the same content.

To solve this problem (Redirect non-www to www) - the best way is by Fix Redundant Hostnames with 301 Redirects.
To do that, we have to edit the .htaccess file by add this command..(on Apache server)
To locate this file, we have to login to CPanel - looking the '<IfModule mod_rewrite.c>' and add the following lines as below and save.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>

This one for sub-domain:-

RewriteEngine On # This line may already exist # Rewrite all requests to the 'www' version
 RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]
 RewriteRule ^.*$ http://www.domainname.com%{REQUEST_URI} [R=301,L] 

Note: domainname must to change to your real domain name


Note:

  • This is for site with https
  • Creating these redirections will also help search engine bots to crawl your website more efficiently

More detail: https://carloseo.com/redundant-hostnames-google-analytics/