Reputation: 3489
I need to redirect some urls.
Formerly I had
http://mysite.com/foobar/?section_id=1234
These are no pointing to
http://mysite.com/foobar/1234
I put the following in my code to handle attempts to visit the old url:
$section_id = filter_input(INPUT_GET, 'section_id', FILTER_SANITIZE_NUMBER_INT);
if ( $section_id ) {
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: ".$_SERVER["SERVER_NAME"]."/foobar/".$section_id );
}
The problem is that after redirecting the url is:
http://mysite.com/foobar
The needed id is not there!
In that if
statement if I do
echo "Location: ".$_SERVER["SERVER_NAME"]."/foobar/".$section_id;
I get:
http://mysite.com/foobar/1234
but then the redirect still fails to include the 1234
part.
Why is this and how to I get it to redirect as desired?
Output of lynx -head -dump http://mysite.com/foobar/1234 (i.e. it looks correct to me)
HTTP/1.1 301 Moved Permanently
Date: Mon, 20 Feb 2012 18:55:09 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.2.6
Set-Cookie: PHPSESSID=c35123456; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Pingback: http://mysite.com/xmlrpc.php
Link: <http://wp.me/mysite>; rel=shortlink
Location: mysite.com/foobar/1234
Vary: Accept-Encoding
Connection: close
Content-Type: text/html; charset=UTF-8
Upvotes: 1
Views: 67
Reputation: 1526
The Location
header must have an absolute and valid URL as its value. I.e. must begin with http://
.
Edit: The Location
header value can also be an absolute path starting with a forward slash /
.
Upvotes: 3