Redirecting non-www to www with htaccess

Redirecting non www to www with .htaccess

Redirecting non-www to www with .htaccess

If you ever point your www. record to another server you will want to setup a redirect for your domain without www. to point to www.

Edit your .htaccess file and add the following code...

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]


This is also useful when using a CDN service such as CloudFlare.

  • 147 Users Found This Useful

Was this answer helpful?

Related Articles

Redirect http to https and www

To forward a website to use both www. and https:// use the following in an .htaccess file:...

Disable error_log via htaccess

Disabling error_log Files via .htaccess   If your error_log file is growing large and...

301 Redirect

This code will redirect all visitors who attempt to access the old-url.html page to the...

What are all these htaccess files?

Understanding .htaccess Files on Your Hosting Account   If you look through your hosting...

What are these htaccess files that keep popping up?

Why .htaccess Files Keep Appearing in My Directories   If you notice .htaccess files...



Save 30% on web hosting - Use coupon code Hosting30