Categories: Snippets

Redirect non-www to www in .htaccess

Below snippet shows how to redirect a site from non-www to www

RewriteCond %{HTTP_HOST} ^onlinenote.in$ [NC]
RewriteRule (.*) http://www.onlinenote.in/$1 [R=301,L]

or

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

or

Here’s the correct solution which supports https and http:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

 

Share

Recent Posts

How to remove .html, .php, etc (extension) from URL?

To remove the .html extension from your urls, you can use the following code in…

5 years ago

Redirect non-www to www and HTTP to HTTPS using htaccess

Below snippet shows how to redirect a site from non-www to www (or viceversa) and…

5 years ago

.htaccess file useful snippets

If you get 404 page not found error after installation. In this case your server…

6 years ago

Common .htaccess file Redirects

Below shows common redirects using htaccess file. [crayon-662f33d0379b7881394789/] The CI .htaccess shouldn't be in the…

6 years ago