Is it possible to append ?ref=myref to all external URLs?

If so I'm guessing I will also need to check if a ? already exists and use & if so.

I'm a WP noob just coming from Joomla, any tips / help would be a very nice warm welcome. :)

Not sure if it can be done on page or .htaccess.

  • Can you clarify what you mean by "all external URLs"? The only "external URLs" are the outbound links that leave your site. Or are you referring to the inbound requests entering your site (that are really "internal" URLs, but with an "external" Referer) - which is how the current answer has interpreted your question? – MrWhite Mar 29 '18 at 8:45
  • 1
    simple answer links from my site to google.com. something like this > stackoverflow.com/questions/11570033/… im unsure where to put this code on WP - lost ! I know where the files are located, broke my site 5 times :) – A Scott Mar 29 '18 at 21:03
  • Ok, in that case, you certainly can't do that using .htaccess (just to eliminate that rabbit hole of thought). – MrWhite Mar 29 '18 at 22:43

You can check if the request headers contain http referer, and then check if the http referer header is from external site, then use .htaccess to rewrite the url internally. But it is not a guarantee way to detect all the external urls as the http referer can be dropped for some reason.


RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !www.example.com [NC]
RewriteRule ^(.+)$ $1?ref=myref [QSA,L]

Line 1 checks if the request header http referer is not empty. Then Line 2 checks if the http referer is from external domain. Line 3 rewrite all the url and append query string ref=myref internally if first two conditions satified.

  • OK thanks a second option, I tried to find the htaccess but cant see it in yoast, i read that some hosts disable certain file access kb.yoast.com/kb/how-to-edit-htaccess-through-yoast-seo i will try ftp, "http referer can be dropped for some reason" will that happen often or is this one of those things that happens every now and then? – A Scott Mar 29 '18 at 21:19
  • Ok i tried this, its not rewriting any urls. possible the hard coded way would be better in any event as every click counts. Thanks – A Scott Mar 29 '18 at 21:46
  • You can't rewrite external/outbound URLs (links) using .htaccess. – MrWhite Mar 29 '18 at 22:40
  • well that explains why it did not work :) did you see my link stackoverflow.com/questions/11570033/…. this code is along the right lines, but i dont know where to add it, if i know where to put it i can work with it. any ideas ? – A Scott Mar 29 '18 at 22:47

Inbound vs Outbound links:

I guess it'll be helpful to clarify these concepts:

Inbound links = links from another website to your own website.

Outbound links = links from your website to another website.

An external link would be any link that points to any domain other than the domain the link exists on.

From your comments, I guess what you're trying to achieve is to modify all your outbound links.

As @MrWhite suggests, you will not be able to change your outbound links by modifying your .htaccess file (it would be helpful, though, if you had to modify your inbound links).

Now, a couple of solutions that can work for you:

Modify your links on the browser (using JavaScript)

This should work (as long as JavaScript is enabled in the user's browser):

document.addEventListener( "DOMContentLoaded", modify_outbound_links);

function modify_outbound_links(){
    anchors = document.getElementsByTagName('a');
    for (let i = 0; i < anchors.length; i++) {
        let p = anchors[i].href;
        if (p.indexOf('yourdomain.com') === -1) {
            anchors[i].href = p + (p.indexOf('?') != -1 ? "&" : "?") + 'ref=myref';

The recommended (and cleanest) option to add some JavaScript to WordPress is using wp_enqueue_script. If you need an easier option, it should work if you just wrap this snippet inside <script> ... </script> and put it inside your footer.php.

Modify your links before they're served to the user (using WP filters)

You can use WordPress filters (functions that are called when a specific event happens).

In your case, you can use 'the_content' filter, which can be used to modify the content of the post after it is retrieved from the database and before it is printed to the screen.

You'd need to add this to your theme's functions.php file:

add_filter( 'the_content', 'myprefix_modify_outbound_links' ); 

function myprefix_modify_outbound_links( $content ) {

    /* here you can search for all external links in $content and modify them as you wish */

    return $content;

Note, this will filter all links in your WordPress posts. You might want to search through your theme's code, just in case there are other external links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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