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

How to Create a Temporary 302 Redirect via htaccess

htaccess & Redirects | Updated March 2026 A 302 redirect sends visitors (and search...

Create a 410 Redirect for Missing Files

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

What are these htaccess files that keep popping up?

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

What are all these htaccess files?

Article Updated This article has been consolidated Everything about .htaccess files is in...

htaccess referral redirect

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



Save 30% on web hosting - Use coupon code Hosting30