Disable ONLY URL auto complete, not the whole canonical URL system

I have a blog with several pages in some category “projects” that are structured/named like this:

  • /projects/project-2012
  • /projects/project-2013
  • /projects/project-2014
  • /projects/project-2015

When a user enters URLs like http://myblog.com/project or even http://myblog.com/proje he/she is redirected to the page /projects/project-2012. (With a 301 Moved Permanentely!)

While I want wordpress to turn URLs resulting in one clearly defined page (e.g. like http://myblog.com/?p=123) into the canonical form, I want to disable only the URL auto-completion for “unclear” URLs that might point to several pages.

My question is: How can I accomplish this?


I also did some research…

  • The accepted answer to the question
    Disable WordPress URL auto complete
    disables the whole canonical URL system. This is not acceptable for
    me.

  • About four years ago something like this popped up on the WordPress bug tracker: https://core.trac.wordpress.org/ticket/8948 While some good solutions (like offering a page “We did not found your URL. But were you maybe looking for one of the following pages?”) were discussed there, the ticket was closed in the end.

  • EDIT: There is actually a newer ticket at https://core.trac.wordpress.org/ticket/16557 which covers exactly what I need. It seems to be targeted for the 4.0 release. And the ticket comments also contain a solution (see below).

2 s
2

Updated (2020-10-29):

The solution has changed with WordPress 5.5. Keeping the old answer for reference below.

Short version: Get my pluign from here:
https://wordpress.org/plugins/disable-url-autocorrect-guessing/

Long Version:

With WordPress 5.5, you can control more precisely if/how the guessing should work. Refer to https://make.wordpress.org/core/2020/06/26/wordpress-5-5-better-fine-grained-control-of-redirect_guess_404_permalink/ for more information. To completely disable guessing, use this code:

add_filter( 'do_redirect_guess_404_permalink', '__return_false' );

If you add this to a new plugin php file (for example in wp-content/plugins/disable-url-autocorrect-guessing.php) you’ll have a nice plugin that you can activate to disable WordPress’ autocorrect “guessing” feature.

To save you the trouble to do this, I’ve updated my plugin; version 2.0 uses this oneliner now. You can get the plugin from here:
https://wordpress.org/plugins/disable-url-autocorrect-guessing/


Old answer (before 2020-10-29):

Okay, after searching a bit more I finally found an answer to my own question hidden in a comment of this feature request ticket: https://core.trac.wordpress.org/ticket/16557 The user nacin suggested to use this code:

function remove_redirect_guess_404_permalink( $redirect_url ) {
    if ( is_404() )
        return false;
    return $redirect_url;
}

add_filter( 'redirect_canonical', 'remove_redirect_guess_404_permalink' );

If you add this to a new plugin php file (for example in wp-content/plugins/disable-url-autocorrect-guessing.php) you’ll have a nice plugin that you can activate to disable WordPress’ autocorrect “guessing” feature.

In order to save you the trouble I actually did this and handed in my plugin at WordPress.org. Once it gets reviewed there, you should be able to download it here:
https://wordpress.org/plugins/disable-url-autocorrect-guessing/


While this is a working solution, the suggested code is somewhat of a hack. Once the feature request in https://core.trac.wordpress.org/ticket/16557 is actually implemented, there will be way better solutions for this as well as much better control over how the guessing should actually be performed.

Leave a Comment