Can I send a HTTP redirect to a url with an anchor?
Asked Answered
S

6

8

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

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

Steam answered 16/8, 2009 at 10:57 Comment(6)
which language are you using?Leafage
Web Server? If you're doing this using mod_rewrite you it won't work as #'s are escapedSkew
@Manni yes I did try that, but had a error in my code, that's why it didn't work. Now it works. @Shiraz I'm using python.Steam
Does that mean that your question is no longer relevant?Forwardness
@Manni are you having a bad day?Steam
No, I'm fine, thanks. Why do you ask?Forwardness
P
6

Yes, you can use the fragment identifier. This was a known bug in the HTTP spec, fixed in subsequent revisions of the spec. See RFC 9110.

Perjured answered 16/8, 2009 at 21:48 Comment(0)
C
4

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.

Corrientes answered 16/8, 2009 at 11:24 Comment(0)
C
4

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.

Ceceliacecil answered 19/8, 2009 at 8:57 Comment(0)
W
2

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

Header(
    "Location: http://en.wikipedia.org/wiki/HTTP#Status_codes",
    true,
    302
);
Whet answered 16/8, 2009 at 11:4 Comment(2)
I know this guy!Dray
I want to do this but if I do this there is a problem. If I go on my link I will redirect to in this case http://en.wikipedia.org/wiki/HTTP#Status_codes/ and the / at the end shows not the anchor point. I coud rename the anchor point to Status_codes/ I want to know if there is a better way to do this.Stanzel
G
1

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

Georgena answered 16/8, 2009 at 11:16 Comment(0)
E
1

While the original RFC 2616 allowed only absoluteURI in the Location header, as the other (older) answers explain, the current RFC 7231 allows any URI-reference, either a full URI (with fragment possibly included), or even a relative URI, resolved against the current effective URI. And it even explicitly describes the behavior of fragments during redirection (if the Location header includes the fragment, it is used, if not, the original fragment is inherited and applied after the redirection).

I.e. yes, you can, it is even officially standardized now.

Ernaline answered 8/2, 2021 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.