2

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:

https://www.mydomain.com.au/search-result/?location=victoria

https://www.mydomain.com.au/search-result/?location=new-south-wales

https://www.mydomain.com.au/search-result/?location=queensland

https://www.mydomain.com.au/search-result/?location=south-australia

https://www.mydomain.com.au/search-result/?location=tasmania

https://www.mydomain.com.au/search-result/?location=northern-territory

Where to redirect to

I want to redirect them to the home page: https://mydomain.com.au/

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.,

https://www.mydomain.com.au/search-result/?location=western-australia

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, https://www.mydomain.com.au/search-result/?location=victoria&weather=part-shade&water=&pasture=

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. After posting this, and also seeing Admiral Noisy Bottom's comment, I've realised doing this with .htaccess rewrite or redirect rules in going to be the most efficient approach. In the interest of upping my PHP and Wordpress specific PHP skills, I would still like to see how this could be achieved on that level. So I am leaving the question here. I've posted a modified version of this question on Stackoverflow asking how to do this with Redirect rules in htaccess.

  • 3
    I'd imagine using Wordpress's rewrite functions would incur unnecessary php overhead, where as .htaccess or nginx rewrite rules would provide performance benefits. I'd be inclined to do rewrites through your http server. I tend to not let php do anything it doesn't have to and allow more suitable processes to do the work. Besides, .htaccess etc are handled much earlier in the process, whereas php is really an end point (if that makes sense). – Admiral Noisy Bottom Apr 4 at 9:32
  • Thanks @AdmiralNoisyBottom. I agree with your thoughts on this. So I have moved this question over to Stackoverflow, as a RedirectRule question. I would still be very interested in seeing how this would be handled in PHP within Wordpress though. So I'll leave the question up for now, with a added note about this. – omega33 Apr 5 at 1:48
1

For normal redirection (i.e. with a URL change in the browser's address bar), and assuming you don't need to access any WordPress data prior to performing the redirection, you should just use the .htaccess to perform the redirection. Because the process is quick(er) that way, just as pointed (by @AdmiralNoisyBottom) in the comment.

In the interest of upping my PHP and WordPress specific PHP skills, I would still like to see how this could be achieved on that level.

In that case, then one way I suggest, is using the parse_request hook, which is also used by WordPress for serving REST API requests at /wp-json/:

  • To allow any location values, except western-australia:

    add_action( 'parse_request', function ( $wp ) {
        // Note that $wp->request doesn't include any leading/trailing slashes.
        if (
            // The request path is /search-result/ or maybe /search-result
            'search-result' === $wp->request &&
            // ..and the query string 'location' is not empty
            ( ! empty( $_GET['location'] ) ) &&
            // ..and the value is not 'western-australia'.
            'western-australia' !== $_GET['location']
        ) {
            wp_redirect( home_url( '/' ) ); // Redirects to the home page.
            exit;
        }
    }, 1 );
    
  • To allow only certain location values:

    add_action( 'parse_request', function ( $wp ) {
        if (
            'search-result' === $wp->request &&
            ( ! empty( $_GET['location'] ) ) &&
            // Allow only certain location values.
            in_array( $_GET['location'], array( 'victoria', 'new-south-wales' ) )
        ) {
            wp_redirect( home_url( '/' ) );
            exit;
        }
    }, 1 );
    

You may also use an early hook such as init, but using the parse_request hook is recommended since WordPress already parsed the request path (from $_SERVER['REQUEST_URI']), so you don't need to manually do the parsing.

But if you'd rather do it manually, then using a hook wouldn't be necessary, because $_SERVER['REQUEST_URI'] is a PHP thing and — if set by the server — it's accessible anywhere on the page.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.