How to Strip Facebook Tracking ID From URL

When there is a link from Facebook to your website, a tracking ID gets added to the link URL.

This is a problem because when people bookmark this link after they followed it from FB, the unwanted tracking ID will be preserved and they may also share this tainted link.

Other problems may arise where the webserver cache gets circumvented leading to a heavier load on the server that generates web pages, needlessly burning more energy and wearing out components and consuming bandwidth.

What we need is for the person viewing your site to have a pure link to bookmark.

So we want to strip off this ID tag, and we may do it via a redirect on our server.

This solution is for the NGINX web server.

We may make use of a map feature in the http section of the nginx.conf file.

Here is the code that we need to add:

  # redirect map in http block - remove fbclid argument from the end
  map $request_uri $redirect_fbclid {
    "~^(.*?)([?&]fbclid=[a-zA-Z0-9_-]+)$"  $1;
  }

This code sets a variable called redirect_fbclid when the request URL contains the fbclid and we may act upon this being set in our server blocks to initiate the redirect as follows:

    # if redirect map is active, do 301 to the new url
    if ( $redirect_fbclid ) {
      return 301 $redirect_fbclid;
    }

I set up a test in my default HTML localhost page to confirm that this approach works, and then implemented it for this website too.

In the browser webmaster tools (accessed via F12) you can observe the 301 (permanent) redirect and the stripping out of this ID in the Network tab.

Credit for the original solution goes to: lynt-smitka/fbclid-nginx.conf