You are here
Home > PHP

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


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

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


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

Get Free Email Updates!

Signup now and receive free offers, discounts & coupon codes

I agree to have my personal information transfered to Mad Mimi ( more information )

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

Leave a Reply

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top