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.

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

// 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

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?
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.
Care for Site Speed?
I think you can’t go wrong with Cloudways, CloudFlare, and Perfmatters.
Perfmatters Cloudways

Resources to host, secure, speed up, rank, and monetize your WordPress sites.