Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL hash is persisting between redirects

For some reason, non IE browsers seem to persist a URL hash (if present) when a server-side redirect is sent (using the Location header). Example:

// a simple redirect using Response.Redirect("http://www.yahoo.com"); Text.aspx 

If I visit:

Test.aspx#foo 

In Firefox/Chrome, I'm taken to:

http://www.yahoo.com#foo 

Can anyone explain why this happens? I've tried this with various server side redirects in different platforms as well (all resulting in the Location header, though) and this always seems to happen. I don't see it anywhere in the HTTP spec, but it really seems to be a problem with the browsers themselves. The URL hash (as expected) is never sent to the server, so the server redirect isn't polluted by it, the browsers are just persisting it for some reason.

Any ideas?

like image 917
chinabuffet Avatar asked Mar 12 '11 15:03

chinabuffet


People also ask

How do you persist URL hash fragments across a login redirect?

For those who don't know, hash fragments were originally implemented in browsers as a way to link to different parts of the same page. By placing an anchor tag somewhere on the page and giving it a name, you could link to that portion of the page by appending the URL with a # followed by the anchor name.

How do I permanently redirect a URL?

A 301 signals a permanent redirect from one URL to another, meaning all users that request an old URL will be automatically sent to a new URL. A 301 redirect passes all ranking power from the old URL to the new URL, and is most commonly used when a page has been permanently moved or removed from a website.

What is the hash of a URL?

In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier.


1 Answers

I suggest that this is the correct behaviour. The 302 and 307 status codes indicate that the resource is to be found elsewhere. #bookmark is a location within the resource.

Once the resource (html document) has been located it is for the browser to locate the #bookmark within the document.

The analogy is this: You want to look something up in a book in chapter 57, so you go to the library to get the book. But there is a note on the shelf saying the book has moved, it is now in the other building. So you go to the new location. You still want chapter 57 - it is irrelevant where you got the book.

like image 142
Ben Avatar answered Oct 11 '22 10:10

Ben