Removing and Redirecting Query Strings

Manuel campos

This is code I generated using AI to solve a problem found on Google Search console.

It redirects a specific query string to the no-query-string version of the URL

You can adjust to meet your site needs.

<script>
// Get the current URL and query string
var currentUrl = window.location.href;
var queryString = window.location.search;

// Check if the URL contains the query string "?swcfpc=1"
if (queryString.indexOf("?swcfpc=1") !== -1) {
  // Remove the query string from the URL
  var newUrl = currentUrl.replace("?swcfpc=1", "");

  // Redirect to the new URL
  window.location.replace(newUrl);
}
</script>

Table of Contents

Where to Add the Script?

You can add this script to your site header or footer.

If you care about your site performance, I recommend adding it to the footer using the free version of a plugin like WPCode

Wanna buy me a Coffee?
JM
About me
I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.
Linktree
Care for Site Speed?
I think you can’t go wrong with Cloudways, CloudFlare, and Perfmatters.
Perfmatters Cloudways

WPSurfer.com

Resources to host, secure, speed up, rank, and monetize your WordPress sites.
Plugins
Services
Connect
LinkedIn
Facebook
LinkTree