url rewriting - How to disable WordPress canonical redirect to nearest matching URL and force 404 Page?

admin2025-01-08  4

WordPress appears to rewrite URLs, displaying Pages under the incorrect URL.

Example. I have a page called "Emotion" with the slug /emotion/

On the front-end it should appear as the following URL ; example/learning/domains/emotion/

That is, there is a child/parent relationship defined within the Pages section of the website, as shown in that URL. Unfortunately, wordpress chooses to show anyone the page, no matter what URL you type in

Examples;

  1. example/search/emotion/
  2. example/this-parent-doesnt-exist/emotion/
  3. example/learning/concepts/emotion/

None of these are real paths. I expect to see a 404 page.

I've done research already, and tried the below two. The first one used to work in WP versions prior to v6. Now neither work.

  1. remove_filter('template_redirect', 'redirect_canonical');

  2. add_filter( 'redirect_canonical', 'disable_redirect_canonical', 10, 2 ); function disable_redirect_canonical( $redirect_url ) { return false; }

How do I disable this rewrite rule?

WordPress appears to rewrite URLs, displaying Pages under the incorrect URL.

Example. I have a page called "Emotion" with the slug /emotion/

On the front-end it should appear as the following URL ; example.com/learning/domains/emotion/

That is, there is a child/parent relationship defined within the Pages section of the website, as shown in that URL. Unfortunately, wordpress chooses to show anyone the page, no matter what URL you type in

Examples;

  1. example.com/search/emotion/
  2. example.com/this-parent-doesnt-exist/emotion/
  3. example.com/learning/concepts/emotion/

None of these are real paths. I expect to see a 404 page.

I've done research already, and tried the below two. The first one used to work in WP versions prior to v6. Now neither work.

  1. remove_filter('template_redirect', 'redirect_canonical');

  2. add_filter( 'redirect_canonical', 'disable_redirect_canonical', 10, 2 ); function disable_redirect_canonical( $redirect_url ) { return false; }

How do I disable this rewrite rule?

Share Improve this question edited Feb 22, 2023 at 20:00 RedWebMan asked Feb 21, 2023 at 5:42 RedWebManRedWebMan 54 bronze badges 3
  • I cannot replicate this behaviour. If I enter the URL for a child page, and change the parent slug, the URL no longer works. Is Emotion just a regular page added in Pages >Add New? Are you using any plugins? Have you tried disabling plugins and switching themes? – Jacob Peattie Commented Feb 21, 2023 at 5:47
  • Huh. Can't replicate it.... That is weird. I use /%category%/%postname%/ as custom permalink structure. Other thing i should have noted, using Elementor Pro theme. – RedWebMan Commented Feb 21, 2023 at 6:51
  • I've checked another website i've built. Same theme, same settings, same plugins (elementor, CPTUI, ACF). issue doesn't present there...The only difference.... one is Wordpress Version 5.x the other Version 6.x. I'd have to put it down to this being a version 6 issue? – RedWebMan Commented Feb 21, 2023 at 7:15
Add a comment  | 

1 Answer 1

Reset to default 0

After doing some research, I have found the answer, albeit with a small limitation.... only shows the page with a trailing slash. This is perhaps technically correct url display so I'll deal with it.

I found the solution buried in this similar Question Disable Wordpress URL auto complete

The CORRECT solution is by David Vielhuber - strangely is not voted as the correct solution. The upvoted solution perhaps worked for older WordPress versions, but no longer since WP v6.x.

To disable WordPress Canonical Redirect (to Nearest Matching URL) and force 404 Page... Add the following code to your child theme functions.php file.

// Disable WordPress canonical redirect to nearest matching URL and force 404 Page
// ---------------------------------
add_filter( 'redirect_canonical', function( $redirect_url ) {
    $url = 'http'.((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']!=='off')?'s':'').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if( $redirect_url !== $url ) {
        global $wp_query;
        $wp_query->set_404();
        status_header( 404 );
        nocache_headers();
    }
    return false; 
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266752a1145.html

最新回复(0)