Google Answers Logo
View Question
 
Q: using PHP to redirect a web page with a 301 status code ( No Answer,   0 Comments )
Question  
Subject: using PHP to redirect a web page with a 301 status code
Category: Computers > Internet
Asked by: coppedahl-ga
List Price: $25.00
Posted: 20 Aug 2006 10:26 PDT
Expires: 19 Sep 2006 10:26 PDT
Question ID: 757868
I need to generate a permanent redirect using HTTP 301.  I am running
the Xitami web server with PHP version 4.4.2.  Here is the script I am
using:

<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.oppedahl.com/index2.htm");
exit();
?>

But when I view the server response using
http://www.rexswain.com/cgi-bin/httpview.cgi , I get a "302 found"
when I really wanted "301 moved permanently" code.

The PHP documentation says two things I don't really understand.  It says:

The second special case is the "Location:" header. Not only does it
send this header back to the browser, but it also returns a REDIRECT
(302) status code to the browser unless some 3xx status code has
already been set.

But I surely think I had previously already set a 301 status code.

The PHP documentation also says:

Note:  Since PHP 4.4.2 and PHP 5.1.2 this function prevents more than
one header to be sent at once as a protection against header injection
attacks.

Which makes me wonder if my two-line PHP script is doomed never to
execute both lines.

Clarification of Question by coppedahl-ga on 20 Aug 2006 12:09 PDT
Oddly if I have only one line in the PHP file:

<?php
// Permanent redirection
header('HTTP/1.1 301 Moved Permanently');
exit();
?>

Then the received header is indeed a 301:

HTTP/1.0·301(CR)(LF)
Content-Length:·65(CR)(LF)
Content-Type:·text/html(CR)(LF)
X-Powered-By:·PHP/4.4.2(CR)(LF)
Server:·Xitami/Pro(CR)(LF)
(CR)(LF)

But if I try to do both the "http/" and the "location", the response
reverts back to the unwanted "302" value.

Request for Question Clarification by sycophant-ga on 29 Aug 2006 00:49 PDT
I have tested this exact setup with Apache and PHP 5.

And it worked exactly as hoped... 

HTTP/1.x 301 Moved Permanently

The PHP manual says this:
"The second special case is the "Location:" header. Not only does it
send this header back to the browser, but it also returns a REDIRECT
(302) status code to the browser unless some 3xx status code has
already been set."

The script I tested is exactly as follows:
<?php
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://localhost/web/test.php');
exit;
?>

To test and record HTTP headers, I used the Firefox plugin LiveHTTPHeaders
http://livehttpheaders.mozdev.org/

It seems like it must be related to the Xitami server - I can't find
any information on what might affect it.

Regards,
Sycophant
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy