Thursday, November 10, 2005

PHP Header 301 Redirect - Moved Permanently

From time to time, pages change location. At times like these, one can use PHP header function with 301 to notify website visitors that the page has moved, assuming the $location contains the new URL.

Here is how you can use PHP header 301 redirect

header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);


More information about header function can be obtained at PHP.net

  post to Del.icio.us

21 Comments:

At 9:08 AM, Blogger Micha said...

Don't forget to
exit;
the current script!

 
At 11:02 AM, Blogger Svetoslav Marinov said...

Will there be a difference in the header if the php is installed as a mod_apache module or a FastCGI one ?

 
At 6:48 AM, Blogger roguebfl said...

How do you do a timed redirect so you can give a human readable message?

 
At 7:02 AM, Blogger Neil Trehearn said...

roguebfl said...
How do you do a timed redirect so you can give a human readable message?


You can't send new headers if content has already been sent so you would have to do some sort of workaround. For example, you could pass a querystring on that would then be picked up by the called script and display a message telling the user that the requested page has moved to the one being shown.

 
At 8:00 AM, Blogger Albert said...

roguebfl said...

How do you do a timed redirect so you can give a human readable message?


Put this on header of a page:

<meta http-equiv="refresh" content="<time in seconds>;<URL TO redirect into>" />

Then do whatever you want on the <body> so that you can show your human readable message

 
At 8:02 AM, Blogger Albert said...

By the way, use the header('HTTP/1.1 301 Moved Permanently'); Before any output (ECHO/PRINT

 
At 8:55 AM, Blogger roguebfl said...

Thank you Albert. I was not sure that the meta refresh tag was compatible with header ('HTTP/1.1 301 Moved Permanently');

 
At 10:13 PM, Blogger Albert said...

Oh, you're right... hmm that made me think how to tell a "moved permanently" and show a redirection message at the same time...

 
At 4:10 AM, Blogger bim said...

How do you do a timed redirect so you can give a human readable message?


The way the headers work makes it impossible to set a message from within the page.

This makes the re-direct much faster as it doesn't have to process much data at all, if you want a message you'll have to set it in the $_GET vars within the redirect link or using a javascript or meta re-direct to link to the new page.

It's not recommended though as search engines update their index's using the http headers so if you did a java/meta re-direct it's likely that to a search engine that page is still there and has just changed content.

If you own the server the url is on you could possibly set it up to leave a message though.

 
At 8:15 AM, Blogger Johan said...

You can also do

header("Location: ", true, 301);

why would you bother with 2 header calls when 1 is enough

 
At 9:57 AM, Blogger DDR Media Services said...

This comment has been removed by the author.

 
At 9:59 AM, Blogger Albert said...

probably by .htaccess:

RewriteRule ^(.+)$ $1?%{QUERY_STRING} [R=301]

or something like that

 
At 10:03 AM, Blogger DDR Media Services said...

We have a php site that changed from .info to .com. All same pages. Is there a way redirect each old page to it's new corresponding page?

 
At 10:08 AM, Blogger Albert said...

@DDR

dude, I just answered your question

"probably by .htaccess:

RewriteRule ^(.+)$ $1?%{QUERY_STRING} [R=301]

or something like that"

 
At 10:13 AM, Blogger DDR Media Services said...

Thank you Albert. Didn't see it the first time apparently.

 
At 10:49 AM, Blogger Albert said...

This comment has been removed by the author.

 
At 10:50 AM, Blogger Albert said...

@DDR you're welcome, but I think it should be like this:

RewriteRule ^(.+)$ http://yourwebsite.info/$1?%{QUERY_STRING} [R=301]

"or something like that"

 
At 1:14 PM, Blogger DDR Media Services said...

That did the trick. It works beautifully. Thanks again!

 
At 8:20 PM, Blogger Blog Man said...

Thank you! it work well

 
At 4:12 AM, Blogger Kyupa Suria said...

Thank you! You saved me from an apache error on online server...now my portfolio is working :)

 
At 4:13 AM, Blogger Kyupa Suria said...

Thank you! You saved me from an apache error on online server...now my portfolio is working :)

 

Post a Comment

<< Home

eXTReMe Tracker