htaccess & Redirects

Redirecting non-www to www with htaccess

Updated January 2026 7,200 views 1 min read
htaccess & Redirects | Updated January 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

Share: X in Reddit f Email
  • 147 Users Found This Useful

Was this answer helpful?

Related Articles

What are these htaccess files that keep popping up?

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

301 Redirect

htaccess & Redirects | Updated September 2026 A 301 redirect is a permanent redirect. It...

Preserving SEO After a Weebly Migration with 301 Redirects

htaccess and Redirects | Updated July 2026 Moving off Weebly changes your URLs, and if you do...

Create a 410 Redirect for Missing Files

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

Disable error_log via htaccess

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



Save 30% on web hosting - Use coupon code Hosting30