http error 301 moved permanently with .htaccess in php

Some time when we transfer or redirect some of our website page to other URL, we face an error http 301 moved permanently error in php. its means our search engine does not find the specified URL so it throws http 301 – moved permanently error on the screen. A page redirect with 301 permanent tells the search engines that new URL has taken place of old URL permanently. So search engines will remove old urls from there indexes.

Here’s the ways, we can redirect from one file to another file.

if you want to redirect page from http://livescript.in/old_file.html to http://livescript.in/new_file.html.

Redirect 301 /old_file.html /new_file.html

Redirect Site to WWW URL

Use this setting if you want to keep your site always running www URL. This also helpful for SEO.

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

Redirect Site to Non WWW URL

If you want to keep your site to run always without www URL. This has there own pros and cons. Like using cookies on site.

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

Redirect Entire Site to New Domain

Ever you want to change your website domain, you can easily redirect all pages from old url to new url.

Redirect 301 / http://newlivescript.in/

Or you can use following settings.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^old_livescript.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldlivescript.in [NC]
RewriteRule ^(.*)$ http://newlivescript.in/$1 [L,R=301,NC]