If you are receiving the redundant hostname notification it means that Google Analytics is tracking your domain www.example.com and example.com independently. This means that your pages will be split across two URLs even if they provide the same content.

www.carloseo.com/redundant-hostnames-google-analytics
carloseo.com/redundant-hostnames-google-analytics
You can prevent this by consolidating both domains into one.
Before proceeding, you have to decide if you prefer to show your domain with or without WWW. There are a few technical differences but in general, it is only a matter of personal preference.
Step 1: Quick Fix for Redundant Hostnames With a Filter in Google Analytics
This will help to consolidate your hostnames in GA quickly.
Redundant Hostnames in Google Analytics
To fix the notification "redundant hostnames" in Google Analytics:
- Go to the admin section.
- Click on Filters on the 3rd column VIEW, then click the red button +Add Filter.
- Enter "Consolidate hostnames" as name and select Custom for filter type
- From the radio button list select Search and replace (you might need to scroll a little).
- Configure the filter depending on how you want your hostname to appear.
- Converting WWW to non-www
- Filter Field: Hostname
- Search String: ^www\.
- Replace String: leave it empty
- Converting non-www to WWW
- Filter Field: Hostname
- Search String: ^yourdomain\.com$
- Replace String: www.yourdomain.com
- Converting WWW to non-www
- Save your filter and continue to the next part (You can click verify this filter before saving to have a quick glance of how it will work)
This will solve the redundant hostname in Google Analytics, however, your site will still have 2 separate URLs for each page, so I recommend following the second step.
Step 2: Fix Redundant Hostnames with 301 Redirects
This part of the guide is more technical. The following configuration files are sensitive so it is important to make a backup before making any changes.
If you can't locate your files or you don't feel comfortable editing them ask your web developer or hosting provider, they should be able to help you without a problem.
After making the changes test them in a new incognito window to see if your site is working fine and all the redirections are working.
Note: Creating these redirections will also help search engine bots to crawl your website more efficiently.
Fix redundant hostnames on Apache server (.htaccess)
To access your .htaccess file, you should go to the root directory in your Cpanel "yourdomain.com/cpanel". Sometimes you have to check "show hidden files".
Once you locate the file add the following lines at the beginning of the file.
Redirect WWW to non-www
For HTTPS sites
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule (.*) https://example.com/$1 [L,R=301] </IfModule>
For HTTP sites
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^www.example.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] </IfModule>
Redirect non-www to WWW
For HTTPS sites
<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>
For HTTP sites
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] </IfModule>
For more information check this advanced guide on htaccess.
Fix redundant hostnames on Nginx
Nginx is a little more complex than the htaccess. Once you locate the configuration file add the following lines:
Redirect WWW to non-www
server { server_name www.example.com; rewrite ^/(.*)$ http://example.com/$1 permanent; }
Redirect non-www to WWW
server { server_name example.com; rewrite ^/(.*)$ http://www.example.com/$1 permanent; }
Fix Redundant Hostnames with WordPress/PHP
If for any reason you can't access your configuration files you can use PHP, this is less efficient than the other two.
Redirect WWW to non-www
<?php if ($_SERVER['HTTP_HOST'] == 'www.example.com'){ header("Location: http://example.com".$_SERVER['REQUEST_URI']); } ?>
Redirect non-www to WWW
<?php if ($_SERVER['HTTP_HOST'] != 'www.example.com'){ header("Location: http://www.example.com".$_SERVER['REQUEST_URI']); } ?>
Step 3: What to Do After Consolidating Your Hostnames
Independently from the method you used, once you have made the necessary changes, go to your Google Analytics click in the redundant hostname notification and click on the blue text "Check Again"
This will change the status to pending verification
And after GA confirms that everything is ok the notification will change to "resolved".
