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 or even 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 /?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: 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 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).
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/project or even http://myblog/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/?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/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/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 Commented Aug 28, 2018 at 7:05
2 Answers
Reset to default 14Updated 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/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/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/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/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. Once it gets reviewed there, you should be able to download it here: https://wordpress/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/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.
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:
- Handling redirect manually, at
template_redirect
. - 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.