0

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?

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? 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

1 Answer 1

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; 
});
1
  • I should add for clarification, for anyone wondering why I want to force a 404, I know there is a majority view that 404 pages are bad for your website. However, If you don't know something is broken (because Wordpress automatically rewrites the url), how do you know to fix it? Since I've implemented above, I've identified a bunch of misconfigured hyperlinks (broken links), and now can immediately fix them. This surely is better for SEO then spoofing content with url rewrites.
    – RedWebMan
    Commented Feb 21, 2023 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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