Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari losing hash params on http redirection

Tags:

I am facing an issue wherein the url fragments are not getting preserved on redirect in Safari as they should be according to the http specifications.


Setting -

`/url1` redirects to `/url2#hash`

`/url2` redirects to `/url3`

Expected behaviour -

Hitting `/url1` should redirect to `/url3#hash`

Observed behaviour -

Chrome/FF - Hitting `/url1` redirects to `/url3#hash`
Safari(11+) - Hitting `/url1` redirects to `/url3`

I did read the issue reported for earlier versions of Safari. I also tried the solutions posted in other SO threads in vain.

Any help is appreciated.

like image 571
Urvashi Gupta Avatar asked Jul 05 '18 06:07

Urvashi Gupta


People also ask

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

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 redirect a URL in Safari?

To configure: Go to the Safari menu, Preferences, Extensions, then select URL Rewrite.


1 Answers

We ran into the same issue today and made some additional observations. We were also able to reproduce the problem with an example Spring Boot app:

@Controller
class RedirectController {

    @GetMapping("/url1")
    String url1() {
        return "redirect:/url2#hash";
    }

    @GetMapping("/url2")
    String url2() {
        return "redirect:/url3";
    }

    @GetMapping("/url3")
    @ResponseBody
    String url3() {
        return "Hello World";
    }
}

When running with Safari 14.1.2 on OSX 10.15.7 (Catalina) hitting /url1/ leads to /url3 without carrying over the fragment (#hash) from /url2. But doing so with Safari 14.1.2 (same version) on OSX 11.5.2 (Big Sur) works. Hitting /url1 leads to /url3#hash.

In any case hitting /url1#this-is-fragment leads to /url3#this-is-fragment.

Our hypothesis is, that Safari (on Catalina) carries over fragments only if they are created in the browser and not on the server. Safari on Big Sur seems to behave like other browsers (e.g. Firefox and Chrome).

Based on that hypothesis one "solution" is a client side redirect, for example via meta refresh:

@Controller
class RedirectController {

    @GetMapping("/url1")
    @ResponseBody
    String url1ClientSideRedirect() {
        return """
            <html>
            <head>
              <meta http-equiv="refresh" content="0;URL='/url2#hash'">
            </head>
            <body></body>
            </html>
            """;
    }

    @GetMapping("/url2")
    String url2() {
        return "redirect:/url3";
    }

    @GetMapping("/url3")
    @ResponseBody
    String url3() {
        return "Hello World";
    }
}

I suppose any client side JavaScript working with window.location would work as well.

like image 185
Sebastian Sprenger Avatar answered Sep 23 '22 06:09

Sebastian Sprenger