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

What is domain propagation?

Domain propagation is the process of updating all of the DNS servers around the world with the...

I changed the DNS pointers to your servers but still can't see my site

It can often take up to 48 hours for the DNS zone transfers to replicate to some ISPs and for...

What is a domain name pointer or parked domain?

Do you want to have multiple domain names point to your website? With a domain name pointer,...

I am having problems with my current registrar transferring the domain to you

If you have unlocked the domain with the current registrar, clicked on the confirmation link in...

DNS Report Tools

To track down DNS related issues there are many websites available to assist you in isolated...