I recently noticed that WordPress tries to auto-complete a URL when it’s not submitted in its entirety. E.g. I have a post URL that looks like this:
http://www.mysite.com/some-post-title
If I browse to the following URL:
http://www.mysite.com/some-post-ti
I can see that the URL is submitted to WordPress but that WordPress is doing a 301 redirect to http://www.mysite.com/some-post-title
.
How can I disable this behavior?
I believe that is the
redirect_canonical
function hooked totemplate_redirect
. You should be able to disable it with:But you should really think about whether you want to do that as it is fairly complicated and performs some important SEO functions:
The following might kill the autocompletion without messing with the SEO component, but I can’t promise that. The code is barely tested as I have never wished to disable this. I’d really have to study
redirect_canonical
to be sure of anything.This seems terribly irresponsible, to have this “guessing” occur automatically. I would be much more open to it if there were some means of defining which was the correct page to go to.
I have numerous pages that are built as a sequence, and this auto-guessing is incorrectly choosing to respond with pages that are (randomly?) somewhere in the sequence, as opposed to the starting page.
UPDATE: This is known behavior, and is being considered here: https://core.trac.wordpress.org/ticket/16557
As a follow up to FitPM’s answer – a plugin has been created that disables the auto-guessing behavior.
It works fine for me on WordPress 4.8 as of August 2, 2017. The plugin is here: https://wordpress.org/plugins/disable-url-autocorrect-guessing/
One note: If one disables redirect_canonical (e.g. with
remove_action('template_redirect', 'redirect_canonical');
), random links like https://tld.com/some-random-string/blogpost also work (the content of https://tld.com/real-category-slug/blogpost is shown).If you want to prevent that and return 404 on those random urls, use something like