How to add & ref = 123 to each link

How can I put at the end of each link on my site without editing each link?

eg www.WebsiteName.com/?ref=123

so if i went in www.WebsiteName.com/aboutus.phpi want to add ?ref=123at the end of the url.

+3
source share
4 answers
var has_querystring = /\?/;

$("a[href]").
    each(function(el) {
        if ( el.href && has_querystring.test(el.href) ) {
            el.href += "&ref=123";
        } else {
            el.href += "?ref=123";
        }
    });
+8
source

Depending on what you mean, without editing each link.

If you mean that you simply do not want to manually add it to your source, you can do this:

$('a[href]').attr('href', function(i, hrf) { return hrf + '?ref=123';});

Or, if you meant that you did not want to do this, you can add a handler .click()that will add a value when the link is clicked.

$('a[href]').click(function( e ) {
    e.preventDefault();
    window.location = this.href + '?ref=123';
});
+3
source

RewriteRule. , . PHP DOM JavaScript

:

# For blank query only
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.php$ /$1.php?ref=123 [L]

# Append to existing query
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)$ /$1&ref=123 [QSA,L]
+1
source
$('a[href]').attr("href", function(){
    this.href + '?ref=123';
});

There will be a very simple method. You may have to add some error checking to make sure you are not destroying other parameters, etc.

0
source

Source: https://habr.com/ru/post/1770758/


All Articles