How to 301 Redirect for index & non-www to www using .htaccessa

0
1823

301 Redirection is very important for our website for redirecting website on one direction because search engine may be index three copies of the same webpage. So, there is a chance of duplicate content and May your rank from search engine may be drop from searches.

301 Redirection is also good for better SEO of a website. You can point your website to only www or without www or to index. We will use .htaccess for redirection of your website just follow our below method and add 301 Redirection on your website.

First of all you need to create .htaccess file on your server and add below codes.

Redirecting non-www to www (root)

Options +FollowSymLinks
RewriteEngine onRewriteBase /RewriteCond %{http_host} ^example.com$ [nc]RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc,L]

Redirecting index.html to www (root)

Options +FollowSymLinks
RewriteEngine onRewriteBase /RewriteCond %{THE_REQUEST} ^.*\/index\.html\ HTTP/RewriteRule ^(.*)index\.html$ http://www.example.com/$1 [R=301,L]

Redirecting index.php to www (root)

Options +FollowSymLinks
RewriteEngine onRewriteBase /RewriteCond %{THE_REQUEST} ^.*\/index\.php\ HTTP/RewriteRule ^(.*)index\.php$ http://www.example.com/$1 [R=301,L]

Redirecting index.html and non-www to www (root)

Options +FollowSymLinks
RewriteEngine onRewriteBase /RewriteCond %{THE_REQUEST} ^.*\/index\.html\ HTTP/RewriteRule ^(.*)index\.html$ /$1 [R=301,L]

RewriteCond %{http_host} ^example.com$ [nc]

RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc,L]

Use the above code to redirect your website for the better SEO of your website. As you know that better SEO help to rank better in search engine.

SHARE