tags:

views:

459

answers:

4

I'm using PHP to redirect a page back to the previous page with the following:

header("Location: {$_SERVER['HTTP_REFERER']}");

This set of pages will only be used by internal users, so I'm not terribly concerned about the fact that the referer will not always be available.

The problem I'm running in to is that if the referer looks like http://subdomain.domain.com/test.php?id=13, the redirect ends up going to http://subdomain.domain.com/.domain.com/test.php?id=13. Notice the additional .domain.com/ in the url.

I've tested by hardcoding the value, and it causes the problem as well. phpMyAdmin seems to suffer the same issue, but only on this particular server.

If this is not an SO question, please move accordingly.

EDIT: per @yaggo

test.php contains only header("Location: http://subdomain.domain.com/test2.php");

curl --head --referer 'http://subdomain.domain.com/' 'http://subdomain.domain.com/test.php'

HTTP/1.1 302 Found
Server: nginx/0.7.64
Date: Fri, 02 Apr 2010 17:21:45 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.2.12-pl0-gentoo
Location: .domain.com/test2.php
A: 

header("Location: ".$_SERVER['HTTP_REFERER']);

Edited:

Check you .htaccess settings or if no solution found u can use preg_replace to remove that last ".domain.com"

but it looks that is a not a php error.

OR use javascript to get the referer address... then use window.location.href = url; to redirect ...

Val
That does the exact same thing. how would this fix the problem.
GSto
actually he had curly brackets there so i thought sticking to the normal, spimple solution is best. but -1 is too harsh as this wasn't an incorrect answer.
Val
+1  A: 

[...] the redirect ends up going to http://subdomain.domain.com/.domain.com/test.php?id=13.

Can you isolate the problem a little bit more? Is that url exactly what is returned by PHP or is it how browser (Chrome?) sees it?

Can you check the actual headers e.g. with curl:

$ curl --head --referer 'http://your-referer' 'http://your-page/'
jholster
+1  A: 

I've recreated both your programs on my server once with

header("Location: http://subdomain.domain.com/some/place");

and once with

header("Location: {$_SERVER['HTTP_REFERER']}");

and both give the corret result

curl --head --referer 'http://subdomain.domain.com/some/place' 'http://subdomain.domain.com/test.php'

HTTP/1.1 302 Found
Date: Fri, 02 Apr 2010 17:48:54 GMT
Server: Apache/2.0.52 (Red Hat)
X-Powered-By: PHP/5.1.2
Location: http://subdomain.domain.com/some/place
Connection: close
Content-Type: text/html

I'm using a different version of PHP and a different webserver, so there's two things to investigate.

bjelli
+1  A: 

It seems that your nginx configuration is causing the problems.

Its totally possible that nginx is modifying the response headers. This is not by default - you could have a configuration that is aimed for it to behave as a reverse proxy etc.

Have you tried testing the redirect on a nginx with its default configuration?

zaf