Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bypass WordPress redirect_canonical for a certain URL

I have a WordPress site and I made a folder inside the WordPress installation folder called en. So suppose if my site URL is example.com which loads my WordPress site, the visitor should can access the contents of en folder by visiting example.com/en/ URL.

But unfortunately the WordPress redirects the visitor to a page with URL like example.com/environmental-advertising/, which is a blog post. As I understood, WordPress's redirect_canonical causing this redirect.

So how can I prevent this type of redirects for a certain URLs like example.com/en/?

*UPD #1: The en folder is another WordPress installation.

like image 800
bobsilon Avatar asked Sep 06 '25 07:09

bobsilon


1 Answers

You can use redirect_canonical to tell the function to not use this url when try to find the page. Here is the code:

add_filter( 'redirect_canonical', 'custom_redirect_canonical', 10, 2 );
function custom_redirect_canonical( $redirect_url, $requested_url ) {
    if( $requested_url == 'http://example.com/en' ) {
        return $requested_url;
    }

    return $redirect_url;
}

Please note the following:

  1. I do not recommend to use such function. It will be better if you change your folder name to something that redirect_canonical will skip.
  2. The above code use hardcoded url which is also not a best practice.
like image 67
Stanimir Stoyanov Avatar answered Sep 10 '25 02:09

Stanimir Stoyanov