It's well known that the URL fragment (the part after the #
) is not sent to the server.
I do wonder though how fragments work when a server redirect (via HTTP status 302 and Location:
header) is involved.
My question is really two-fold:
If the original URL had a fragment (
/original.php#foo
), and a redirect is made to/new.php
, does the fragment part of the original URL simply get lost? Or does it sometimes get applied to the new URL?
Will the new URL ever be/new.php#foo
in this case?Regardless of the original URL, if the server redirects to a new URL with a fragment (
/new.php#foo
), will the fragment get "honored"? Or does the server really have no business interfering with the fragment at all -- and will the browser therefore ignore it by simply going to/new.php
??
Safari 5 and IE9 and below drop the original URI's fragment if a HTTP/3xx redirect occurs. If the Location header on the response specifies a fragment, it is used.
IE10+, Chrome 11+, Firefox 4+, and Opera will all "reattach" the original URI's fragment after following a 3xx redirection.
Test page: http://www.webdbg.com/test/redir/fragment/.
See further discussion of this issue at http://blogs.msdn.com/b/ieinternals/archive/2011/05/17/url-fragments-and-redirects-anchor-hash-missing.aspx
Update 2014-Jun-27:
RFC 7231, Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content, has been published as a PROPOSED STANDARD. From the Changelog:
The important points from Section 7.1.2. Location:
This should clearly answer your questions.
Update END
this is an open (not specified) issue with the current HTTP specification. it is addressed in 2 issues of the IETF httpbis working group:
#6 allows fragments in the
Location
header. #43 says this:this leads to the most browser compatible and future proof (because this issue will eventually get standardized) answer to your question:
A: fragments from original URLs get discarded.
B: fragments from the
Location
header are honored.Just to let you know, here you can find proper spec. by w3c defining how all should behave: http://www.w3.org/TR/cuap#uri - clause 4.1 - see below: