{"id":1743,"date":"2020-09-07T07:09:27","date_gmt":"2020-09-06T23:09:27","guid":{"rendered":"https:\/\/www.intelliwolf.com\/?p=1743"},"modified":"2020-09-07T07:09:29","modified_gmt":"2020-09-06T23:09:29","slug":"stop-wordpress-partial-match-redirection","status":"publish","type":"post","link":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/stop-wordpress-partial-match-redirection\/","title":{"rendered":"How To Stop WordPress Partial Match Redirection"},"content":{"rendered":"\n
We had an unusual situation where we deleted a page and wanted to serve a 404 error page in its place.<\/p>\n\n\n\n
Instead, anyone who went to that URL was redirected to another page on the site.<\/p>\n\n\n\n
That hadn't happened with any other URLs. Then it happened with another page we deleted.<\/p>\n\n\n\n
There were no redirect plugins in place and no redirects in the .htaccess file.<\/p>\n\n\n\n
On further investigation, WordPress was guessing that if we typed in a partial match for a longer URL, we must have meant that longer URL.<\/p>\n\n\n\n
That's when I came across canonical redirects.<\/p>\n\n\n\n
How to stop WordPress partial match redirection:<\/strong> on WordPress 5.5 and above, add a filter to 'do_redirect_guess_404_permalink<\/em>' that returns false. For earlier versions, add a filter to 'redirect_canonical'<\/em> that returns false if is_404()<\/em> is true or returns the URL.<\/p>\n\n\n\n Since WordPress 5.5.0, if you want to stop WordPress guessing the permalink and return a 404 error, use this code:<\/p>\n\n\n\n On older versions of WordPress, we need to connect earlier in the sequence and check if we're on a 404 page.<\/p>\n\n\n\n This code still works in newer versions of WordPress, so if you need something that covers before and after an upgrade, use this code.<\/p>\n\n\n\n Here's how it looks in code:<\/p>\n\n\n\n or if you prefer something more compact, you could use:<\/p>\n\n\n\nWordPress 5.5+ Solution<\/h2>\n\n\n\n
add_filter( 'do_redirect_guess_404_permalink', 'stop_redirect_guess');\n\nfunction stop_redirect_guess() {\n return false;\n}<\/code><\/pre>\n\n\n\n
Solution For Older WordPress Versions<\/h2>\n\n\n\n
add_filter( 'redirect_canonical', 'stop_redirect_guess' );\n\nfunction stop_redirect_guess( $url ) {\n if ( is_404() ) {\n return false;\n }\n return $url;\n}<\/code><\/pre>\n\n\n\n
add_filter( 'redirect_canonical', 'stop_redirect_guess' );\r\n\r\nfunction stop_redirect_guess( $url ) {\r\n return is_404() ? false : $url;\n}<\/code><\/pre>\n\n\n\n