views:

277

answers:

5

Is it possible to send a response with 302 status code to a url like this:

http://mysite.com/something/#somethingelse

+1  A: 

There appears to be no problem in doing so from PHP:

Header(
    "Location: http://en.wikipedia.org/wiki/HTTP#Status_codes",
    true,
    302
);
Sebastian P.
A: 

Yes. It's the browser which doesn't send the hash to the server, not the other way around.

joebert
+2  A: 

Following the HTTP specification, the value for the Location header field must be an absoluteURI value. And that is according to the RFC 3986 (they just changed the name from absoluteURI to absolute-URI):

absolute-URI  = scheme ":" hier-part [ "?" query ]

So theoretically the fragment is not allowed as part of the value. But the browsers can handle it.

Gumbo
+1  A: 

Yes, you can use the fragment identifier. This is a known bug in the HTTP spec.

See http://trac.tools.ietf.org/wg/httpbis/trac/ticket/6.

Julian Reschke
A: 

With strict reading RFC2616 does not allow fragments in Location header values, since they are not part of absolute URIs. However, with the IETF's HTTP rewrite draft this was fixed.

Recently Julian put up a comparison how browsers handle URI fragments (that's what the HTML anchor tags deal with) in the Location header: http://www.greenbytes.de/tech/tc2231/redirects.html

So the answer ist: Yes, you can put fragments in Location header URIs.

mkoeller