Removing and Redirecting Query Strings

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

Manuel Campos, English Professor

Manuel Campos

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.






© 2024 WP SURFER • Made with Love in Costa Rica