8

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

  • this core URL guessing feature also messes with SEO and SEO tools !! – Mau Aug 28 '18 at 7:05
11

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.

  • Wish I could upvote this thrice... – KalenGi Aug 7 '15 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 Jul 4 '18 at 10:37
  • no longer works in v5+ – nodws Jun 25 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. Jun 26 at 8:15
  • Oh it was a conflict with YOAST's redirects – nodws Jun 27 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.

  • 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. May 20 '14 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 May 20 '14 at 14:00
  • Actually I did find a solution on my own: wordpress.stackexchange.com/a/144970/51898 – Hauke P. May 20 '14 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 May 20 '14 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. May 20 '14 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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