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

htaccess referral redirect

htaccess & Redirects | Updated 2026 You can use .htaccess to redirect visitors based on...

htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable

htaccess & Redirects | Updated March 2026 The "pcfg_openfile: unable to check htaccess...

Show longer file names in directory/folder index

Updated 2026 Quick Answer To show longer file names in Apache's directory listing...

Complete Guide to htaccess on Apache

htaccess & Redirects | Updated 2026 The .htaccess file is one of the most powerful tools...

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