Permanent (301) redirection in PHP

Following is the code for permanent (301) redirect using php.
A better approach would be to use .htaccess but there are situations where .htaccess could not be used

therefore using following code might help

[code]
@header("HTTP/1.1 301 Moved Permanently");
@header("location: http://www.mywebsite.com");
[/code]

Additionally if you want to redirect your home page to always open using www. which might be usefull when using

cookies or for SEO. Add an if condition to code

[code]
if (!preg_match(‘/www..*?/’, $_SERVER[‘HTTP_HOST’]))
{
@header("HTTP/1.1 301 Moved Permanently");
@header("location: http://www." . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’]);
}

[/code]

Get Free Email Updates!

Signup now and receive free offers, discounts & coupon codes

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge