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 redirect 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 PHP header function can be obtained at PHP.net
Internet
21 Comments:
Don't forget to
exit;
the current script!
Will there be a difference in the header if the php is installed as a mod_apache module or a FastCGI one ?
How do you do a timed redirect so you can give a human readable message?
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.
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
By the way, use the header('HTTP/1.1 301 Moved Permanently'); Before any output (ECHO/PRINT
Thank you Albert. I was not sure that the meta refresh tag was compatible with header ('HTTP/1.1 301 Moved Permanently');
Oh, you're right... hmm that made me think how to tell a "moved permanently" and show a redirection message at the same time...
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.
You can also do
header("Location: ", true, 301);
why would you bother with 2 header calls when 1 is enough
This comment has been removed by the author.
probably by .htaccess:
RewriteRule ^(.+)$ $1?%{QUERY_STRING} [R=301]
or something like that
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?
@DDR
dude, I just answered your question
"probably by .htaccess:
RewriteRule ^(.+)$ $1?%{QUERY_STRING} [R=301]
or something like that"
Thank you Albert. Didn't see it the first time apparently.
This comment has been removed by the author.
@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"
That did the trick. It works beautifully. Thanks again!
Thank you! it work well
Thank you! You saved me from an apache error on online server...now my portfolio is working :)
Thank you! You saved me from an apache error on online server...now my portfolio is working :)
Post a Comment
<< Home