2

Currently, I have a script which is accessible through the following domain

http://me.domain.com/path/index.php

If certain condition met, I will perform redirection back to itself by using

header('Location: index.php');

After redirection, most major browsers will end up at

http://me.domain.com/path/index.php

still. However, for some 3rd party vendor's browsers (For sim card simulation purpose), they will end up at (Take note on the missing path)

http://me.domain.com/index.php

I was wondering, it is because the 3rd party vendors doesn't implement their browsers correctly? Or, it is OK for different browsers yields different behaviour when dealing with redirect?

I realize if I use the following code,

// $_SERVER['PHP_SELF'] is /path/index.php
header('Location: '.$_SERVER['PHP_SELF']);

It will work in all browsers, without missing path.

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

2 Answers2

6

well manual says:

Most contemporary clients accept relative URIs as argument to ยป Location:, but some older clients require an absolute URI including the scheme, hostname and absolute path. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:

in short ALWAYS use full URI to cover all cases

  • Thank you @Dagon. I always thought header redirection is browser independent. Now I know it isn't. โ€“ Cheok Yan Cheng Dec 21 '15 at 04:25
  • [This answer](http://stackoverflow.com/a/25643550/628267) to a similar question points out the RFC change where relative URLs were made valid in Jun 2014. โ€“ John C Dec 21 '15 at 04:25
  • @CheokYanCheng your welcome, don't forget to accept an answer :-) .. I post a comment on absolute URI for location every time i see it - lots of people just use relative because it works for their browser test. โ€“  Dec 21 '15 at 04:48
0

The specification (14.30) says that Location header must be followed by an absolute URI. You are sending a relative one, so it is against the specification and both ways of understanding it are equally correct.

Technically an absolute URI should have the protocol and the hostname also, not only the path, so even though the latter works better, it is still not correct.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74