Redirect non-www to www using .htaccess

URL redirection is an important part of the web development, one of those is that both www and non-www URLs are technically different and that it could hurt your SEO if you keep both around.

For example, the domains https://techbiriyani.com and https://www.techbiriyani.com are technically different. However, if you click on either they will redirect you to the same place. It helps Google and other search engines to index only one and to not split results. In this tutorial we are showing you how to redirect your non-www URLs to www, or vice-versa, using htaccess.

What best for me

I must say it totally depends upon your views on how your website should look. Traditionally site owners have opted to redirect to the www version of their site. You may have seen old sites like Google, Facebook, Wikipedia, and LinkedIn are redirecting to the www version while new sites like Techcrunch, Twitter, Dribble uses Non-www version. It’s just a trend to use www or non-www version for our website. The non-www version also known as Naked domain is using widely for the current trend.

To redirect all requests, create a .htaccess file in your app’s web root directory:

public/.htaccess

with your preferred domain type:

How to redirect www to non-www

The following .htaccess file will redirect requests for www.example.com to the same path example.com.

RewriteCond %{HTTP_HOST} =www.example.com
RewriteRule (.*) http://example.com/$1 [R=302,L]

If you are using WordPress, be sure to place these rules above the WordPress rules.

replace example.com with your domain.

How to redirect non-www to www

The following .htaccess file will redirect requests for example.com to the same path www.example.com.

RewriteCond %{HTTP_HOST} =example.com
RewriteRule (.*) http://www.example.com/$1 [R=302,L]

If you are using WordPress, be sure to place these rules above the WordPress rules.

replace example.com with your domain.

Redirect Everything

You may also redirect all requests to your website unconditionally. Since no condition is applied each and every requests will redirect to the domain given by you.

RewriteCond %{HTTP_HOST} =OLDDOMAIN
RewriteRule (.*) http://NEWDOMAIN/$1 [R=302,L]

[su_note note_color=”#f9002c” text_color=”#ffffff” radius=”5″]Don’t use this if the domain you are redirecting to is not part of the same app, otherwise you will create a redirect loop and requests will fail with an internal server error.[/su_note]

Keep in mind that 302 is temporary and 301 is permanent. You can always change a 302 redirect to 301 redirects once you have confirmed your redirect is working as expected and you want it to be cached by browsers. If you start with an incorrect 301 redirect, your redirects will stay broken by any browsers as they cached the incorrect redirect.

You can always change the 302 to a 301 after you have confirmed your redirect is working as expected and you are ready for it to be cached by browsers. A 302 redirect is temporary and a 301 redirect is permanent. If you start with a 301 redirect and it’s incorrect, your redirects will stay broken by any browsers that saw the incorrect redirect as those browsers will be caching the incorrect redirect. Make sure the domains you are redirecting both to and from have been added to your app in ServerPilot. 

For example, if you redirect from www both example.com and www.example.com must be added in the Domains tab of your app.

Follow TechBiriyani on FacebookTwitter to get all the latest updates.

Founder
View posts

A passionate blogger and a tech enthusiast who is always keen and awaiting to know about fully fledged new technologies who's currently using Zenfone 3 as a part of daily routine. You can spot me @ #facebook, #twitter & #Google +

Leave a Comment