Friday, November 28, 2014

Redundant hostnames solution for google analytics

Google Analytics recent update showing an unresolved issue: Redundant Hostnames.

Property http://www.example.com is receiving data from redundant hostnames. Consider setting up a 301 redirect on your website, or make a search and replace filter that strips "www." from hostnames. Examples of redundant hostnames: example.com, www.example.com.

The problem is that google see your website as 2 identical websites one with the www. and one without it.

To fix this problem i found the below solution by modifying the .htaccess file.

For Apache, copy these rules in the .htaccess file located in the wwwroot directory: (Choose only one!)

#########################
# redirect no-www to www
#########################

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]

-- or --

#########################
# redirect www to no-www
#########################

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

These two ways will redirect the url to one address.

4 comments: