5 Apr 2020

// // Leave a Comment

How To 301 Redirect From Non-WWW to WWW using .htaccess

301 Redirection

If you are a blogger or a developer who mostly works on blogs, websites, you will definitely have faced this situation to have the website with www prefix to your domain.

There's no official proof that having www prefixed to the domain has any improved SEO effects, yet I have seen most of the websites to have www domains compared to non-www domain.

Here's this post that will help you do a 301 Redirect from non-www to www domain using .htaccess file.


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

The above snippet of code will do a 301 redirection of the non-www url to www url.

Note : Replace example.com with your domain name

Another way is to have a generic htaccess snippet of code to redirect the url from non-www to www.


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

This piece of code will take up any domain url that's non-www and redirect to www domain.

Add either of the above code snippet to your website's .htaccess file

HTTPS urls can be redirected in the same way as above by replacing the http to https.

Let me know in the comments, if the above code snippet worked for you.

Pic Credit. : Unsplash

0 comments:

Post a Comment