Redirect a list of URLs to another URL, using functions.php


What I wish to do

I have a number of WordPress URLs I need to redirect, along with a 301 permanent redirect header being sent to browser.

The URLs to redirect are:

Where to redirect to

I want to redirect them to the home page:

I am not sure whether it’s best to test for all six of those location= strings, or to simply test for the one location= string that is not to redirect.

The one that is not to redirect is ?location=western-australia. E.g.,

Additional considerations

Note that there are other /search-result/ URLs that have different variables in the query strings, such as ?weather=... or ?water=.... For example,

AS seen in that example, it’s also possible multiple variables will be in the query string, such as ?location=tasmania&weather=&water=moderate&pasture=.

So I need to test for the presence of the above listed locations= irrespective of whether or not it has other variables after it. The location= variable will always be the first in the query string.

I am thinking it may be as simple as testing for /search-result/ AND victoria; tasmania; northern-territory; etc. in the URL.

A different approach?

Would it make sense to do this using an .htaccess redirect, as opposed to having WordPress do it? I am not sure of the advantages or disadvantages of each approach.

, omega33 3 years 2020-04-04T04:52:01-05:00 0 Answers 93 views 0

Leave an answer