33

Is this relative location html header absolutely compatible with all browsers at all platforms? Any standards ?

Location: some_script.php?la=2&po=2030

I mean, will it always redirect to some_script.php at the current dir or not?

abrahab
  • 2,430
  • 9
  • 39
  • 64
  • 1
    as it's PHP, the browser won't even know about the header, it just notices it is being redirected, because PHP is executed on the server. – 11684 May 10 '12 at 20:18
  • 3
    @11684: The client's browser will absolutely receive a Location header. How else do you think it decides to change its location? – webbiedave May 10 '12 at 20:21
  • 1
    @11684 Your wrong. PHP is executed on the server, yes. But PHP send the header to the browser (client) and the client do whatever he want with it. – David Bélanger May 10 '12 at 20:23
  • possible duplicate of [How widely supported are scheme-relative URIs in HTTP 301 redirects](http://stackoverflow.com/questions/4387289/how-widely-supported-are-scheme-relative-uris-in-http-301-redirects) – Gumbo May 10 '12 at 20:26
  • @DavidBélanger Doesn't PHP tell the webserver to send the header? O.o – John V. May 10 '12 at 20:31
  • 1
    @DavidBélanger I did not know that, thanks for learning me something! – 11684 May 10 '12 at 20:36
  • @AlexLunix doesn't Apache interpret the PHP? – 11684 May 10 '12 at 21:09
  • @11684 PHP interprets the PHP, then forwards the result to apache to serve. (Correct me if I'm wrong) – John V. May 10 '12 at 22:34
  • 1
    possible duplicate of [Is a 302 Redirect to relative URL valid, or invalid?](http://stackoverflow.com/questions/8250259/is-a-302-redirect-to-relative-url-valid-or-invalid) – Dave Hughes Jan 27 '15 at 17:58

1 Answers1

33

The standard would be this:

header('Location: http://www.mywebsite.com/yourpage.php?id=32', TRUE, 302);

But to answer your question, yes it will redirect to the page X in the current folder if you don't put a slash at first or a complete URL.

Here's an idea I would suggest you do for every website you do. In your primary file (the main php file you use like config or whatever), create something like that :

define('URL', 'http://www.mywebsite.com/');

So when you create a redirection, a link or whatever, you do this :

header('Location: '.URL.'yourpage.php?id=32', TRUE, 302);

EDIT: November 2017. As pointed by @jordanbtucker below, the HTTP spec has been updated in June 2014 (this post is from 2012) to allow relative URIs in the Location header.

David Bélanger
  • 7,400
  • 4
  • 37
  • 55
  • 1
    yes, I am already use **relative** variant and its work for me, but I doubt that it does not work for all users. I can make add part of url to get absolute path, but its will give some troubles for debbugging this sctipt at local machine, because on the we some_script.php always at http://domain.com/some_script.php and its not the problem to add SERVER_HOST to url, but on the local machine script at subfolder... – abrahab May 10 '12 at 20:20
  • 1
    Ok. Then I suggest tou take a look at http://tools.ietf.org/html/rfc2616#section-10.3.3 . The good way, the best way and the way it is suppose to work is you need to supply the full URL. – David Bélanger May 10 '12 at 20:21
  • @abrahab I don't know understand why it's a problem to have the complete URL on a local machine sub-folder ? – David Bélanger May 10 '12 at 20:57
  • 15
    The HTTP spec has been updated to allow relative URI's for the value of the Location header. http://tools.ietf.org/html/rfc7231#section-7.1.2 – jordanbtucker Dec 05 '14 at 00:12