Hosting Guides

WordPress www to Non-www Redirect With .htaccess

Every WordPress site is reachable with or without the www prefix, which means the same site can live at two addresses at once. Visitors can type example.com or www.example.com and land on the same homepage, the same posts, and the same pages. That sounds harmless, but search engines see two versions of every page, and that split causes real problems. Link value gets divided between the two versions, duplicate content warnings appear, and analytics reports become harder to read.

The fix is to pick one canonical host and redirect the other one to it permanently. In this guide you will learn how to set up a WordPress www to non-www redirect with .htaccess, and you will also get the reverse rule if you prefer the www version instead. The whole job takes about ten minutes, it needs no plugin, and it uses a 301 permanent redirect so search engines move all ranking value to the version you keep.

Why Should You Pick One Canonical Host?

A canonical host is the one version of your domain. Every other version redirects to it. When you pick one host and stick to it, every backlink, every social share, and every search result points at the same address, so Google does not split your ranking signals.

There is no SEO advantage to www over non-www, or the other way round. The shorter non-www version is cleaner, easier to say out loud, and easier to fit on a business card. The www version looks more traditional and some older systems expect it. What matters is consistency, not the choice itself.

What Is a 301 Redirect and Why Does It Matter?

A 301 redirect is a permanent redirect. When a browser or a search engine asks for the old address, the server answers with a 301 status code and the new address. The browser then loads the new address, and Google transfers almost all of the ranking value from the old URL to the new one.

A good host redirect also preserves the full path and the query string. A visitor who clicks an old link to www.example.com/blog/my-post should land on example.com/blog/my-post, not on the homepage. If the redirect drops the path, visitors hit the wrong page and old backlinks lose their value.

Step 1: Back Up Your .htaccess File First

Before you change anything, back up your current .htaccess file. The .htaccess file controls how Apache serves your site, and a single typing mistake can bring the whole site down with a 500 error. A backup turns a scary mistake into a thirty second fix, because you can simply restore the old file and start again.

  • Open your hosting file manager or connect with FTP and go to the folder where WordPress is installed, usually public_html.
  • Show hidden files, because files that start with a dot are hidden by default in most file managers.
  • Download a copy of .htaccess to your computer and keep it somewhere safe before you edit anything.
  • If your site already has redirect rules, read them first so your new rules do not conflict with them.

If anything goes wrong after you save the file, do not panic. Upload your backup copy, overwrite the broken file, and the site will come back immediately. If you are not sure how to edit the file safely, our guide to WordPress file permissions in cPanel explains how to work with site files without breaking access.

Step 2: Redirect www to Non-www With .htaccess

This is the rule most site owners choose. It catches every request that arrives with a www host and sends it to the same address without www. It works for the homepage, for posts, for pages, and for any URL with extra parameters. Add this block to your .htaccess file, above the WordPress block, and save it.

# Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Here is what each line does. RewriteEngine On switches the Apache rewrite engine on. The RewriteCond line checks whether the requested host starts with www, and the NC flag makes the match case insensitive. The RewriteRule line rebuilds the address without www, keeps the requested path, and sends a 301 redirect. The L flag tells Apache to stop processing further rules. You can read the full syntax in the official Apache mod_rewrite documentation.

What If You Prefer www Instead?

Some site owners prefer to keep the www version as canonical. That is a valid choice, and the rule is just as simple. Use this block instead of the one above, never both at the same time. If you add both blocks, the two rules will fight each other and your site will fall into a redirect loop.

# Redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

This version checks whether the host does not start with www, and if so it adds www to the front of the same host and path. Again, the rule keeps the full path and query string, and it sends the visitor to HTTPS in one hop. Use exactly one of these two blocks, never both.

Where Exactly Do the Rules Go in .htaccess?

Placement matters more than most tutorials admit. Your redirect rules must go above the block that starts with # BEGIN WordPress. WordPress rewrites that block every time you save your permalink settings, and anything you place inside it can be wiped out without warning.

Open .htaccess and look for the line that says # BEGIN WordPress. Paste your redirect block directly above it, with a blank line between your block and the WordPress block. Save the file, then test the redirect at once. If your site uses a caching plugin, clear the cache after saving.

Alternative: Use the Redirects Tool in cPanel

If editing .htaccess feels too risky, cPanel offers a built in Redirects tool that writes the rule for you. Log in to cPanel, open Domains and then Redirects, and choose the domain you want to fix. Set the type to Permanent (301), enter the source address with www, and enter the destination address without www.

What About Nginx and Cloudflare?

The .htaccess file only works on Apache and LiteSpeed servers. If your site runs on Nginx, .htaccess is ignored completely, so the rules in this guide will do nothing. On Nginx you need a server block rule instead, and on managed Nginx hosting you should ask your host to add it for you.

Cloudflare adds another layer. If your DNS goes through Cloudflare, you can use a page rule or a redirect rule to send www traffic to non-www before it ever reaches your server. That works well, but do not run a Cloudflare redirect and an .htaccess redirect at the same time, because two rules doing the same job can create a chain. Pick one layer and keep the other one clean.

Make Sure SSL Covers Both Hosts

A redirect to HTTPS only works if your SSL certificate covers both the www and the non-www host. Most free certificates from Let us Encrypt cover both versions automatically. But if your certificate covers only example.com, a visitor who types https://www.example.com will see a scary security warning before the redirect ever runs, and many of them will leave.

Check your certificate in your hosting panel and confirm that it lists both hosts. If it covers only one version, ask your host to reissue it with both names, which is normally free. This is also a good time to confirm that your whole site already runs on HTTPS, because the host redirect and the HTTPS redirect should work together in a single hop.

Update the WordPress Address and Site Address

The server redirect is only half the job. WordPress itself must also use the canonical host, or it will keep generating internal links, image URLs, and admin links with the wrong version. Go to Settings, then General, and set both address fields to the same canonical host you chose, including https.

How to Test Your Redirect Properly

Testing in your own browser is not enough, because browsers cache redirects aggressively. A browser that cached the old behaviour will keep showing it even after you fix the server, which makes debugging very confusing. Use tools that show you the raw response instead, so you can see exactly what the server sends.

  • Run curl in a terminal with the command curl -I https://www.example.com and look for a 301 status code and a Location header pointing to the non-www address.
  • Test a deep page too, for example curl -I https://www.example.com/a-post, and confirm it redirects to the same post on the non-www host.
  • Use an online redirect checker to confirm there is exactly one hop from the old address to the final address.
  • Open Google Search Console and check that the canonical version is the one Google has indexed.

If the test shows a 301 and the correct Location header, the redirect works. If it shows a 302, a chain, or no redirect at all, check the rule placement and the spelling of the host names. Small typos in .htaccess cause most failures, and the backup you made at the start lets you restore the file and try again.

How to Avoid Redirect Chains and Loops

A redirect chain happens when one redirect points to another redirect instead of to the final page. A common chain is www to non-www over HTTP, then HTTP to HTTPS, which costs two hops. Chains slow the site down, waste crawl budget, and dilute a little ranking value at every hop. The rules in this guide avoid this by sending the visitor straight to the final HTTPS non-www address in one hop.

A redirect loop is worse. It happens when rule A sends the visitor to address B and rule B sends the visitor back to address A, so the browser spins forever and finally shows an error that the page is redirecting too many times. Loops usually come from running two conflicting rules at once, for example a www to non-www rule in .htaccess and a non-www to www rule in Cloudflare, or a plugin redirect that fights the server rule. The cure is to keep exactly one canonical redirect, in one place.

Once the redirect is live and tested, check Search Console again after a few days. Google will gradually move the indexed URLs to the canonical version, and your traffic reports will show one clean set of pages instead of two. For a broader cleanup, our guide on how to optimize your WordPress database without a plugin is a useful next step, and you can learn how to create a WordPress staging site without a plugin to test future changes safely. If your site does not run on HTTPS yet, read our guide on how to redirect HTTP to HTTPS in WordPress without a plugin first.

Frequently Asked Questions

Should I choose www or non-www for WordPress?

Either choice is fine for SEO, so pick the one you prefer and use it consistently. Non-www is shorter and cleaner, while www looks more traditional. Once you choose, redirect the other version with a 301 and update both WordPress address fields.

Will a www to non-www redirect hurt my SEO?

No, a correct 301 redirect protects your SEO. Google moves almost all ranking value from the old host to the new one, and it removes the duplicate content problem. Problems only appear with a chain, a loop, or a temporary 302, so test for a single 301 hop.

Where do I put the redirect rules in .htaccess?

Place the rules above the block that starts with # BEGIN WordPress. WordPress rewrites that block whenever you save permalinks, so anything inside it can be deleted without warning. Rules above the block stay in place.

Why is my site showing too many redirects after the change?

A too many redirects error almost always means two rules are fighting each other. Check for a conflicting rule in a redirect plugin, in Cloudflare page rules, and in .htaccess, and keep only one canonical redirect in one place.

Last updated: November 2026.

Hostinger - Get 20% OFF

Habib ur Rehman

Hello! I'm the founder of VU Tech Geek. As a BSCS graduate and experienced WordPress professional, I'm passionate about technology and education. My Virtual University journey has given me valuable insights and knowledge, which I love to share with fellow students.… More »

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker