Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header Location relative path compatibility

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?

like image 950
abrahab Avatar asked May 10 '12 20:05

abrahab


People also ask

How is Location header set?

The Location response header indicates the URL to redirect a page to. It only provides a meaning when served with a 3xx (redirection) or 201 (created) status response.

How do I find header location?

To check this Location in action go to Inspect Element -> Network check the response header for Location like below, Location is highlighted you can see. Supported Browsers: The browsers are compatible with the HTTP Location header are listed below: Google Chrome.

Are used for files that have moved and usually include a location header indicating the new address?

300–399 Values in the 300s are used for files that have moved and usually include a Location header indicating the new address. 400–499 Values in the 400s indicate an error by the client. 500–599 Codes in the 500s signify an error by the server.


1 Answers

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.

like image 141
David Bélanger Avatar answered Sep 28 '22 08:09

David Bélanger