{"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

WordPress 5.5+ Solution<\/h2>\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

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

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

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

or if you prefer something more compact, you could use:<\/p>\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

That's the same functionality as the previous, just as a ternary operator<\/a>.<\/p>\n\n\n\n

Strict Match Guessing<\/h2>\n\n\n\n

In WordPress 5.5+ you can also control whether the guess is strict or not using this code:<\/p>\n\n\n\n

add_filter( 'strict_redirect_guess_404_permalink', 'strict_redirect_guessing');\n\nfunction strict_redirect_guessing() {\n  return true;\n}<\/code><\/pre>\n\n\n\n

This to be useful in is where you have a folder structure and you only want to redirect when the full slug is present.<\/p>\n\n\n\n

For example, with strict guessing turned to true<\/em>, domain.com\/hello<\/em> will no longer redirect to domain.com\/2020\/09\/hello-world\/<\/em> but domain.com\/hello-world<\/em> will redirect.<\/p>\n\n\n\n

If you have strict guessing turned to false (or delete your code), domain.com\/hello<\/em> will redirect to domain.com\/2020\/09\/hello-world\/<\/em>.<\/p>\n\n\n\n

The same applies to contractions. With strict guessing turned off, domain.com\/find\/sample<\/em> will redirect to domain.com\/sample-page\/<\/em> but it will not redirect if you turn strict guessing off.<\/p>\n\n\n\n

The way the strict guessing works is the SQL uses post_name LIKE [slug]%<\/em> as default or post_name = [slug]<\/em> for strict matching.<\/p>\n","protected":false},"excerpt":{"rendered":"

We had an unusual situation where we deleted a page and wanted to serve a 404 error page in its place. Instead, anyone who went to that URL was redirected to another page on the site. That hadn’t happened with any other URLs. Then it happened with another page we deleted. There were no redirect<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"yoast_head":"\nHow To Stop WordPress Partial Match Redirection<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Stop WordPress Partial Match Redirection\" \/>\n<meta property=\"og:description\" content=\"We had an unusual situation where we deleted a page and wanted to serve a 404 error page in its place. Instead, anyone who went to that URL was redirected to another page on the site. That hadn't happened with any other URLs. Then it happened with another page we deleted. There were no redirect\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/\" \/>\n<meta property=\"og:site_name\" content=\"Intelliwolf\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-06T23:09:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-06T23:09:29+00:00\" \/>\n<meta name=\"author\" content=\"Mike Haydon\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Haydon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/\"},\"author\":{\"name\":\"Mike Haydon\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343\"},\"headline\":\"How To Stop WordPress Partial Match Redirection\",\"datePublished\":\"2020-09-06T23:09:27+00:00\",\"dateModified\":\"2020-09-06T23:09:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/\"},\"wordCount\":385,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\"},\"articleSection\":[\"General\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/\",\"url\":\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/\",\"name\":\"How To Stop WordPress Partial Match Redirection\",\"isPartOf\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#website\"},\"datePublished\":\"2020-09-06T23:09:27+00:00\",\"dateModified\":\"2020-09-06T23:09:29+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.intelliwolf.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"General\",\"item\":\"https:\/\/www.intelliwolf.com\/category\/general\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How To Stop WordPress Partial Match Redirection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.intelliwolf.com\/#website\",\"url\":\"https:\/\/www.intelliwolf.com\/\",\"name\":\"Intelliwolf\",\"description\":\"WordPress, Web Design and Coding Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.intelliwolf.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\",\"name\":\"Intelliwolf\",\"url\":\"https:\/\/www.intelliwolf.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png\",\"contentUrl\":\"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png\",\"width\":300,\"height\":100,\"caption\":\"Intelliwolf\"},\"image\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343\",\"name\":\"Mike Haydon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g\",\"caption\":\"Mike Haydon\"},\"sameAs\":[\"https:\/\/intelliwolf.com\/about-mike-haydon\/\"]}]}<\/script>\n","yoast_head_json":{"title":"How To Stop WordPress Partial Match Redirection","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/","og_locale":"en_US","og_type":"article","og_title":"How To Stop WordPress Partial Match Redirection","og_description":"We had an unusual situation where we deleted a page and wanted to serve a 404 error page in its place. Instead, anyone who went to that URL was redirected to another page on the site. That hadn't happened with any other URLs. Then it happened with another page we deleted. There were no redirect","og_url":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/","og_site_name":"Intelliwolf","article_published_time":"2020-09-06T23:09:27+00:00","article_modified_time":"2020-09-06T23:09:29+00:00","author":"Mike Haydon","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mike Haydon","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/#article","isPartOf":{"@id":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/"},"author":{"name":"Mike Haydon","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343"},"headline":"How To Stop WordPress Partial Match Redirection","datePublished":"2020-09-06T23:09:27+00:00","dateModified":"2020-09-06T23:09:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/"},"wordCount":385,"commentCount":2,"publisher":{"@id":"https:\/\/www.intelliwolf.com\/#organization"},"articleSection":["General"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/","url":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/","name":"How To Stop WordPress Partial Match Redirection","isPartOf":{"@id":"https:\/\/www.intelliwolf.com\/#website"},"datePublished":"2020-09-06T23:09:27+00:00","dateModified":"2020-09-06T23:09:29+00:00","breadcrumb":{"@id":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.intelliwolf.com\/stop-wordpress-partial-match-redirection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.intelliwolf.com\/"},{"@type":"ListItem","position":2,"name":"General","item":"https:\/\/www.intelliwolf.com\/category\/general\/"},{"@type":"ListItem","position":3,"name":"How To Stop WordPress Partial Match Redirection"}]},{"@type":"WebSite","@id":"https:\/\/www.intelliwolf.com\/#website","url":"https:\/\/www.intelliwolf.com\/","name":"Intelliwolf","description":"WordPress, Web Design and Coding Tutorials","publisher":{"@id":"https:\/\/www.intelliwolf.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.intelliwolf.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.intelliwolf.com\/#organization","name":"Intelliwolf","url":"https:\/\/www.intelliwolf.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/","url":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png","contentUrl":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png","width":300,"height":100,"caption":"Intelliwolf"},"image":{"@id":"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343","name":"Mike Haydon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g","caption":"Mike Haydon"},"sameAs":["https:\/\/intelliwolf.com\/about-mike-haydon\/"]}]}},"_links":{"self":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts\/1743"}],"collection":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/comments?post=1743"}],"version-history":[{"count":3,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts\/1743\/revisions"}],"predecessor-version":[{"id":1892,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts\/1743\/revisions\/1892"}],"wp:attachment":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/media?parent=1743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/categories?post=1743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/tags?post=1743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}