Redirecting non-www to www with htaccess

htaccess & Redirects | Updated 2026

Choosing between www and non-www for your domain is a personal preference, but you should pick one and redirect the other. This prevents duplicate content issues and ensures all your traffic goes to a single canonical URL.

01. Redirect non-www to www

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

Replace yourdomain\.com with your actual domain (escape the dot with a backslash).

02. Redirect www to non-www

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

03. Combined with HTTPS

To force both HTTPS and www (or non-www) in one rule set:

RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
Order Matters

Put the HTTPS rule first, then the www rule. This avoids double redirects (which are slower and can cause issues with some browsers). For the HTTPS redirect, you can also use cPanel's Force HTTPS toggle. See HTTP to HTTPS Redirect.

If redirects are not working or causing loops, see Nginx and .htaccess Redirect Issues. For the complete .htaccess reference, see .htaccess Guide.

Redirect Issues?

Open a Support Ticket

Quick Recap

  1. Pick www or non-www and redirect the other
  2. Use 301 redirects for permanent changes
  3. Combine with HTTPS in the same .htaccess
  4. HTTPS rule first, then www rule
  5. Test in incognito - Browsers cache 301 redirects

URL canonicalization · Last updated March 2026 · Browse all htaccess articles

  • 147 Users Found This Useful

Was this answer helpful?

Related Articles

What are these htaccess files that keep popping up?

htaccess & Redirects | Updated 2026 If you keep finding .htaccess files appearing in your...

Redirect http to https and www

Updated 2026 Quick Answer To redirect HTTP to HTTPS and non-www to www (or vice versa),...

Disable error_log via htaccess

htaccess & Redirects | Updated 2026 The error_log file in your public_html directory...

When I Upload an htaccess File It Disappears

htaccess & Redirects | Updated March 2026 Your .htaccess file is there. You just can't...

Create a 410 Redirect for Missing Files

htaccess & Redirects | Updated 2026 A 410 (Gone) status code tells search engines that a...



Save 30% on web hosting - Use coupon code Hosting30