HTACCESS Redirect WWW to Non WWW

Introduction

This comprehensive guide covers everything you need to know about setting up an htaccess redirect www to non www for your WordPress site or any website. If you are a website owner or manage a WordPress site, understanding how to redirect www to non-www (or vice versa) is crucial for SEO, branding, and user experience. Both www and non-www versions of your website can exist simultaneously, which can lead to duplicate content and split search engine authority. Using .htaccess for redirects is a common practice that ensures a consistent URL structure and helps consolidate your site’s ranking power. In this guide, you’ll learn why redirecting matters, how to choose between www and non-www, and get a step-by-step walkthrough to set up the redirect in your .htaccess file.

Why is it Important to Redirect WWW to Non WWW in WordPress

Redirecting www to non-www (or vice versa) is essential for several reasons:

  • SEO Benefits: Search engines like Google treat ‘www.example.com’ and ‘example.com’ as two separate websites. If both versions are accessible, your inbound links and ranking power can be split between them. Redirecting ensures all authority is consolidated to your preferred version, boosting SEO performance.

  • Consistency and Branding: Maintaining a single URL format (either www or non-www) strengthens your brand identity and presents a unified image to visitors.

  • Avoiding Content Duplication: Without redirection, search engines may index both versions, causing duplicate content issues that can harm your rankings.

  • Improved User Experience: Redirecting ensures visitors always land on your preferred version, regardless of what they type, reducing confusion and improving navigation.

  • Analytics and Tracking Accuracy: Using a single URL version prevents data from being split across two sites, giving you more accurate analytics for better decision-making.

Both www and non-www versions can exist simultaneously, but a www to non-www redirect ensures a consistent URL structure for your website.

Now that you understand the importance, let’s look at which format to choose.

Table of Contents

WWW or Non-WWW: Which Should I Go for?

When deciding between www and non-www, consider your branding, technical needs, and personal preference. Both options are valid, but consistency is key. Once you choose, stick with it across your entire site and set up the appropriate redirect.

Ready to implement your choice? Next, let’s walk through how to set up the redirect using your .htaccess file.

How to Force WWW or Non-WWW with .htaccess Redirect

Using .htaccess for redirects is a common practice that gives you control over how visitors and search engines access your site. Below is a step-by-step guide to redirect www to non-www using .htaccess.

Step 1: Locate Your .htaccess File

  • The .htaccess file is usually located in your website’s root directory (often called public_html).

  • If you do not see a .htaccess file in your root directory, you may need to create one, especially if you recently used a one-click installer to set up WordPress. Make sure hidden files are visible in your file manager settings.

access file manager in cPanel
Open public html folder

Step 2: Enable Hidden Files

show hidden dot files in cPanel

Step 3: Back Up .htaccess

If your site displays missing visuals after editing this file, be prepared to find and fix broken images in WordPress as part of your troubleshooting.

Step 4: Add Redirect Code

  • Open the .htaccess file in a text editor or, if you prefer working inside WordPress, use the built-in editors described in our guide on how to edit text in WordPress effectively.

  • Place the following code before any other rules in the file:

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

  • Note: Replace http://%1 with https://example.com if your site uses HTTPS, and replace example.com with your actual domain name.

Explanation of the code:

  • RewriteEngine On enables URL rewriting.

  • RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] checks if the incoming request includes www.

  • RewriteRule ^(.*)$ http://%1/$1 [R=301,L] performs a permanent (301) redirect to the non-www version.

Step 5: Save and Test

  • Save the .htaccess file and upload it back to your server if you edited it locally.

  • Clear your browser cache to ensure you see the updated redirect.

  • Test the redirect by entering the www version of your site in your browser. It should automatically forward to the non-www version.

edit htaccess to redirect www to non www or vice versa.

Now that you’ve set up the redirect, let’s address some common questions.

Frequently Asked Questions

What is the difference between www and non-www URLs?

Both www and non-www URLs refer to the same website, but search engines treat them as separate entities unless you set up a redirect. Choosing one and redirecting the other ensures consistency and avoids duplicate content.

Why should I redirect www to non-www URLs?

Redirecting consolidates your site’s authority, prevents duplicate content, and improves SEO and analytics accuracy.

How do I .htaccess redirect www to non-www URLs?

Follow the step-by-step guide above to add the redirect code to your .htaccess file, ensuring you back up the file and test the redirect after saving, and remember you can also disable a problematic WordPress theme via FTP if theme conflicts arise.

Where do I find my .htaccess file?

If you primarily work from the dashboard, it helps to first get familiar with the WordPress admin panel interface and menu structure.

The .htaccess file is typically in your website’s root directory (public_html). Enable hidden files in your file manager to view it.

What do I do if I don’t have a .htaccess file?

If you don’t see a .htaccess file, you can create one in your root directory. Make sure hidden files are visible in your file manager settings, as you may also need FTP access when fixing issues like a blank WordPress admin panel caused by configuration errors.

Do I need to do anything else after redirecting www to non-www URLs?

After setting up the redirect, clear your browser cache and test the redirect to ensure it works correctly, and stay alert for unexpected behavior that could signal hacked redirect malware on your WordPress site.

Conclusion

Redirecting www to non-www using .htaccess is a simple yet powerful way to improve your website’s SEO, branding, and user experience, especially if you have recently converted a static HTML site to WordPress and want consistent URLs from the start. By following the steps in this guide, you ensure a consistent URL structure and avoid common pitfalls like duplicate content and split analytics. Always back up your .htaccess file before making changes, and test thoroughly to confirm everything works as expected, keeping your login secure with best practices such as knowing how to change your WordPress admin password safely.