To redirect non-www to www using your .htaccess file, add the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Combined with HTTP to HTTPS redirect
If you also want to force HTTPS and redirect non-www to www, here’s a combined version:
</p>
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<p data-start="259" data-end="348">
OR (More optimized version with both conditions combined)
Replace example.com with your actual domain name:
RewriteEngine On
# Redirect to https://www.example.com
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Notes:
-
The
[NC]flag makes the rule case-insensitive. -
Always replace
example.comwith your real domain. -
Make sure your SSL certificate supports both
wwwand non-wwwdomains.
Let me know if you want to do the reverse (www to non-www) or need help with something specific!
How to redirect http to https in IIS
