0

I'm trying to determine if the user is browsing PWA on server side. On client side I can check if the browser mode is standalone via JavaScript and detect PWA, but on server side this is not an option. So I'm using simple query parameter for that. Start URL in manifest has query param isPwa appended and every time user opens PWA, it goes to https://example.com/?isPwa. What I need now is to keep that parameter while user browses PWA, so I need to set isPwa parameter to next URL that user opens if the referer URL already had isPwa parameter, but my code goes to redirection loop and I'm unable to identify the cause of this. Here's my code:

function addIsPwaQueryArg() {
    $referer = wp_get_referer();
    if (strpos($referer, 'isPwa') !== false) {
        $location = remove_query_arg('isPwa');
        wp_redirect(add_query_arg('isPwa', '', $location));
        exit;
    }
}
add_action('template_redirect', 'addIsPwaQueryArg');

Can someone tell me what's wrong with my code and why is it not working?

Thanks!

1
  • 1
    You shouldn't rely on the referrer, it can be modified by the user, spoofed, and lots of browsers and extensions strip it for privacy purposes
    – Tom J Nowell
    Commented Mar 29, 2021 at 21:40

1 Answer 1

0

You're not passing the actual querystring to remove_query_arg(). After some quick testing, I think your $location value is probably an empty string, and the location you're attempting to redirect to is ?isPwa.

The 2nd parameter to remove_query_arg() should be the $referer value.

function addIsPwaQueryArg() {
    $referer = wp_get_referer();
    if (strpos($referer, 'isPwa') !== false) {
        $location = remove_query_arg('isPwa', $referer );
        wp_redirect(add_query_arg('isPwa', '', $location));
        exit;
    }
}
add_action('template_redirect', 'addIsPwaQueryArg');

However: I'm not sure that's the root cause of your redirect loop. It looks like you're checking to see if isPwa is in the querystring, removing it if it is, and then adding it back in (with a blanked value) as you redirect. Is this your intent?

3
  • Redirection is happening on the correct URL as I see in the browser address bar, so If I'm on https://example.com/?isPwa and click the link, it redirects on https://example.com/link/?isPwa but browser displaying redirected too many times error. That's what I'm trying to achieve: Checking to see if isPwa was in previous URL querystring and then adding it back (with a blanked value) if it was in next URL.
    – DaftPlug
    Commented Mar 29, 2021 at 22:03
  • Check your webserver logs to see what redirection is actually happening. You should see a bunch of 301 or 302 statuses pop up in there when you load the page; that might give you some insight as to why you're getting redirected too many times. (It's possible you'll need to ask your web hosting provider for assistance.)
    – Pat J
    Commented Mar 30, 2021 at 0:08
  • I think after the redirection, the logic see new referer with ?isPwa param and redirects it again, resulting in the redirection loop.
    – DaftPlug
    Commented Mar 30, 2021 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.