php - How to add &ref=123 every link - Stack Overflow

How would i be able to put the follow on the end of every link of my website with out editing every lin

How would i be able to put the follow on the end of every link of my website with out editing every link?

e.g www.WebsiteName/?ref=123

so if i went to www.WebsiteName/aboutus.php i want it to add ?ref=123 onto the end of the url.

How would i be able to put the follow on the end of every link of my website with out editing every link?

e.g www.WebsiteName./?ref=123

so if i went to www.WebsiteName./aboutus.php i want it to add ?ref=123 onto the end of the url.

Share edited Oct 21, 2010 at 15:44 Sean Vieira 160k34 gold badges320 silver badges296 bronze badges asked Oct 21, 2010 at 15:36 RickstarRickstar 6,19921 gold badges57 silver badges74 bronze badges 1
  • Keep in mind that of course the end user needs to have javascript enabled for any of these solutions to work. You could change the links server side using output buffering, and either phpquery or regex. – mellowsoon Commented Oct 21, 2010 at 22:23
Add a ment  | 

4 Answers 4

Reset to default 8
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";
        }
    });

Depends on what you mean by without editing every link.

If you mean that you just don't want to manually add it in your source, you could do this:

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

Or if you meant that you didn't want to have to do that, you could attach a .click() handler that will add the value when the link is clicked.

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

I would use a RewriteRule. It's the easiest and most reliable way in my opinion. That is to say not parsing the page with PHP or fighting DOM load issues with JavaScript

Here's an example:

# 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]
$('a[href]').attr("href", function(){
    this.href + '?ref=123';
});

Would be a very simple method. You'd probably have to add some error checking to make sure you're not ruining any other parameters, etc.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742265626a4411729.html

相关推荐

  • php - How to add &ref=123 every link - Stack Overflow

    How would i be able to put the follow on the end of every link of my website with out editing every lin

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信