12

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

1
  • this core URL guessing feature also messes with SEO and SEO tools !!
    – Mau
    Commented Aug 28, 2018 at 7:05

2 Answers 2

14

Updated Answer (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.

8
  • Wish I could upvote this thrice...
    – KalenGi
    Commented Aug 7, 2015 at 4:38
  • Inspired me as I was having issue in paged redirection. I was planning to remove_filter(). But now bypassing only specific case where I am having trouble. Just in-case anyone interested about my issue: wordpress.stackexchange.com/questions/307670/…
    – Parixit
    Commented Jul 4, 2018 at 10:37
  • no longer works in v5+
    – nodws
    Commented Jun 25, 2019 at 17:17
  • @nodws: What are you referring to? I'm using my plugin with the code snippet on 5.2.2 and it still works fine.
    – Hauke P.
    Commented Jun 26, 2019 at 8:15
  • Oh it was a conflict with YOAST's redirects
    – nodws
    Commented Jun 27, 2019 at 1:14
0

Unfortunately redirect_canonical() is 400+ lines of code (and keeps growing from release to release), which is not particularly structured to be controlled by purpose. It's all or nothing deal that cannot be flexibly configured.

From practical point of view your best options are:

  1. Handling redirect manually, at template_redirect.
  2. Preventing redirect as redirect_canonical hook if target it came up with is not desirable.

In either case you'll have to develop out the logic of what exactly makes undesirable redirect.

5
  • Huh, that is unexpectedly very unfortunate. :-( My definition of a undesirable redirect is pretty simple: Every URL that cannot be resolved to exactly one target (but multiple or none) is undesirable and should result in a 404.
    – Hauke P.
    Commented May 20, 2014 at 13:48
  • @HaukeP. logic responsible for that is part of redirect_guess_404_permalink() and it doesn't make such distinction, just takes the first match SQL produces :(
    – Rarst
    Commented May 20, 2014 at 14:00
  • Actually I did find a solution on my own: wordpress.stackexchange.com/a/144970/51898
    – Hauke P.
    Commented May 20, 2014 at 14:33
  • @HaukeP. I misunderstood you a bit about "exactly one target", because some cases of fuzzy matching will technically resolve to only one match, but it's not the same as getting rid of fuzzy stuff in general :)
    – Rarst
    Commented May 20, 2014 at 14:37
  • Yeah, actually while writing my last comment I stopped for a second to think about again in the fear of being misunderstood. :) So it looks like I should have written my comment (and question) more precise after all.
    – Hauke P.
    Commented May 20, 2014 at 14:46

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.