Recently I added Google Analytics to one of my projects. After a few weeks when Google had enough data, I started to get the following notification.

Redundant Hostnames

Property example is receiving data from redundant hostnames.

The error looks something like the following image.

Redundant Hostname Notification

In this post we are going to resolve this. So let’s start by understanding what are these so called Redundant Hostnames

If you are seeing this error in your google analytics account, then it means that your website can be accessed with one or more hostnames. This generally happens if your website is being accessed both with and without “www” prefix. Another possible hostname that your site possibly can be accessed from is the server’s IP address.

Why Google Complains About It

One of the major reason Google complains about redundant hostnames is because it is not a good practice to have your website accessible via multiple hostnames for optimising your website for search engines. In your site is accessible with multiple hostnames, Search Engines will have a hard time figuring out unique content from your site and index it.

How to get rid of the Redundant Hostnames ?

As explained above, search engines may have problems indexing your website if it is accessible with multiple hostnames. So to fix this, We’ll need to make sure that our website is only accessible by only one consolidated hostname. To do so, we’ll need to permanently redirect (301 Redirect) all other hostnames to the one that we’d like to use for our site.

301 permanent redirect. But how ?

301 permanent redirects can be directly handled in your server’s configuration file. Another way to accomplish this can be to write a re-write rule in your “.htaccess” file. For the re-write rule, we’ll need to make sure that mod_rewrite is enabled on your web server.

Example Nginx Configuration

In the following sample nginx configuration we will show you how to 301 permanent redirect your website from non www to www. i.e. from “http://example.com” to “http://www.example.com”

If you use SSL i.e. your website uses “https” protocol or uses both “http” and “https” protocol you can look at the configuration below to redirect all hostnames to the correct one including the “https” ones

If you’d like us to add more information about how to fix this using Apache configuration or .htaccess re-write rules, Please comment and we’ll add the required information to this post.

For more nginx configuration options to redirect from www to non www or vice-versa, see this stackoverflow topic.