Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect to a mobile site but keep the hash tag?

I'm looking to have it so that when a user loads our page it checks to see if they're on a mobile device and then it'll redirect them to a mobile version of our site, but keep the original hash tag from the link they followed. I've tried setting the new location with the hash tag in javascript and it works in Chrome but doesn't work in Safari. I've read that this is just something Safari does. Is there any work around to this?

like image 488
Cory Schulz Avatar asked Dec 08 '11 19:12

Cory Schulz


People also ask

How do I redirect a mobile site?

In order to help Googlebot discover the location of your site's mobile pages, Google recommends websites must should: Add a rel="alternate" tag on the desktop page which points to the corresponding mobile URL. Add a rel="canonical" tag on the mobile page that points to the corresponding desktop URL.

How do you hashtag 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. When you use a URL with a # , it doesn't always go to the correct part of the page or website.

Why is there a hash in my URL?

A hash sign (#) in a URL is referred to as a fragment. Historically, URL fragments have been used to automatically set the browser's scroll position to a predefined location in the web page. In that sense, if a URL refers to a document, then the fragment refers to a specific subsection of that document.


3 Answers

We actually just got a very similar bug report from our customers.

For us the scenario involved a hashtag in the form of #quicklogin/abc123 and only appeared when viewing the site in Safari. This was part of the initial URL that the client would load and it would present them with an alternate login screen. When going directly to the URL in Safari, the browser removed the entire tag and ignored it.

When we changed this to something like #quicklogin/test (or any other hash containing only alpha characters) it worked fine. It also worked fine when loading the site and then manually applying the hash tag in two separate steps.

So our conclusion is that there may be something wrong with hashtags containing numeric values in Safari.

like image 55
Eric Rini Avatar answered Oct 05 '22 15:10

Eric Rini


You have to do this either by redirecting with javascript (because javascript can read the hash value) or by conditionally returning different html based on the user agent.

Browsers are supposed to preserve the hash fragment through a 302, but often don't (see 3 year old webkit bug below) and otherwise hash fragments are not sent to the server so they can't be dealt with manually.

https://bugs.webkit.org/show_bug.cgi?id=24175

like image 28
Evan Avatar answered Oct 05 '22 16:10

Evan


You should escape '#' symbol and everything would be OK

like image 27
user3683979 Avatar answered Oct 05 '22 16:10

user3683979