Skip to content
Advertisement

Keep query string for external urls

A user can be sent to the main website by some referral id, using a query string:

www.site.com/?ref=XXX

And when they click on a link to a login area, this query string must go with that external link:

https://internal.site.com?ref=XXX

I am working with wordpress, so I cannot append ?ref=<?php echo $_GET['ref']?> to that link.

I have already changed .htaccess to:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php - [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,QSA]
</IfModule>

Every link back to the main website keeps the query strings, but external urls do not.

www.site.com?ref=XXX [original]
www.site.com/go/reds?ref=XXX [query strings are kept]
https://internal.site.com [query strings are removed]

The QSA flag in htaccess should do the trick, but it’s not working for external links.

Advertisement

Answer

You can do this by adding this script at the end of your page. This code will pass variables to every links on client side.

<p><a href="/about/">Internal</a></p>
<p><a href="https://google.com">External</a></p>
<p><a href="/post/?id=1">Link with query string</a></p>

<script>
    var index = window.location.href.indexOf('?')
    if(index != -1){
        var querystring = window.location.href.slice(index + 1)
        var tagA = document.getElementsByTagName('a');

        for(var i = 0; i < tagA.length; i++){
            var href = tagA[i].getAttribute('href');

            href += (href.indexOf('?') != -1)? '&' : '?';
            href += querystring;

            tagA[i].setAttribute('href', href);
        }
    }
</script>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement