301 Redirect from non-www to www for WordPress to resolve Redundant Hostnames

301 Redirect domain.tld to www.domain.tld via .htaccess - TutLinks
301 Redirect to resolve redundant hostnames – TutLinks

Sometimes few websites are set up in such a way that the example.com and www.example.com will be treated as different websites. Although both URLs have same content and represent the same website, tracking few analytics becomes cumbersome when the domain with and without www does not resolve to the same hostname.

Resolve Redundant Hostnames warning from Google Analytics

Google will warn you to resolve redundant hostnames with the following message, in case you have Google Analytics tracking configured for your website.

resolve redundant hostnames
Resolve redundant hostnames. An acutal warning from Google Analytics for the site mfrepublic.com

Property example.com is receiving data from redundant hostnames. Some of the redundant hostnames are:
Redundant hostnames are counted as separate rows in reports, so hits that are going to the same page on your site from different hostnames will be split into multiple rows. With data split across multiple rows, traffic to specific pages will appear lower than it actually is.

To avoid this problem, consider setting up a 301 redirect from one of your redundant hostnames to the other, or create a search-and-replace filter that strips β€œwww.” from hostnames.

Google Tag Assistant Recordings can help you verify that your redirect is setup correctly, or that your filter is working as intended.

This is how the warning from Google Analytics will look like

To resolve this problem of redundant hostnames, you need to add a 301 redirect to the website. So the question is where to make changes in order for the domains with and without www resolve to the same hostname?

Approach to add 301 redirect in .htaccess

One of the best and most reliable way to fix this, is in the .htaccess configuration file of the website. In this tutorial we will see how to add a 301 redirect from non-www to www for WordPress to resolve the redundant hostnames.

Before you proceed on a live site to add non-www to www redirect

Sometimes your site is live and many visitors might be accessing it. A simple mistake in the .htaccess may cause damage to both the website and its reputation. In case you are not sure about what you are doing, it is suggested to consult a Software or DevOps professional to fix it for you.

Take backup before editing .htaccess

The .htaccess file is generally located in the root of the website’s directory. Before you make any modifications to the .htaccess file, ensure you take the backup of the .htaccess file.

Code to add in .htaccess to redirect non-www to www URL

Add the following lines that actually redirect your website to www if the non-www URL is visited. To edit the file you can open .htaccess in any basic code editor or notepad of your choice. In case you have access only to Secure Shell (SSH) you can use vim or nano editor based on your comfort to edit .htaccess file. You must ensure you have sudo privileges to edit .htaccess file.

# BEGIN 301 redirect from example.com to www.example.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI}  [L,R=301]
# END 301 redirect from example.com to www.example.com

If you do not have SSL (Secure Socket Layer) enabled for your website then replace https with http in line 5 of the code snippet provided above.

Once you have updated the .htaccess file save and close the file. Clear the browser cache. In case you have any plugin installed to cache your website cache, purge the website cache too.

Open any browser and enter the URL of your website without WWW in it. You should see the website actually redirected to www URL. If you are using Chrome browser, the URL address bar of Chrome automatically hides the http protocol and the www sub-domain. To ensure that, you need to place the mouse pointer in the URL address bar and click there until you see the cursor. Now ensure you see the www in the URL.

Now that the domains with and without www are resolving to the same hostname, you can update the Google Analytics warning as the problem is fixed. Google will take sime time to identify your fix and mark the redundant hostname warning to resolved.

Video Tutorial on adding 301 Redirect from non-www to www

In case you want to see how to add a 301 redirect from non-www to www to a real website, you may want to take a look at this video tutorial below that details on How to add 301 Redirect from non www to www for WordPress.

How to add 301 Redirect from non ww... x
How to add 301 Redirect from non www to www for Wordpress

Navule Pavan Kumar Rao

Hi, I am am Engineering Manager with product development experience in the health care services, banking, finance, corporate tax, and automobile domains. I am a published author for the book 'Getting Started with V Programming'. I pursued Executive M.Tech in Data Science from the Indian Institute of Technology (IIT), Hyderabad. I also pursued Executive MBA in IT specialization from the Indian School of Business Management and Administration, and also holds a B.Tech in Electronics and Communication Engineering from Vaagdevi Institute of Technology and Science. I am a Microsoft Certified Professional (MCP) and Microsoft Certified Technology Specialist (MCTS). I played key roles in identifying business requirements and converting them into viable products with the help of his vast software development skills. I also played a key role in the architecting, development, and deployment of software applications that become a part of the CI/CD pipeline to cloud platforms such as Azure, GCP, AWS and also to on-premise infrastructures. Check out my book "Getting Started with V Programming" Checkout my book here: https://amzn.to/3FOsxSm
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
SEO Company
2 years ago

Awesome post! Keep up the great work! πŸ™‚

1 year ago

You keep here an nice tutorial of .htaccess usage in 301. Very good , keep up posting posts like this please

wpdiscuz Β  wpDiscuz