WordPress attempts to guess a redirect URL for a 404 request
Today I found out that one of the client’s WordPress sites redirects to a post page for a request that is supposed to return a 404 page. The request to https://example.com/a
brought you to the https://example.com/about
page.
At first, I thought this came from a redirection plugin or Yoast SEO Premium plugin. However, there was no redirection on any of those plugins.
Investigating response headers
Because none of the plugins defined the redirection, I opened the web developer tools on the Google Chrome I used. I clicked the Network
tab and checked the response headers for the request to https://example.com/a
Here’s the result that I filtered:
location: https://example.com/about
x-redirect-by: WordPress
That x-redirect-by
showed WordPress itself did the redirect. If another plugin like Yoast does it, its value would be:
x-redirect-by: Yoast SEO Premium
Well, here is the link to the documentation where WordPress introduces this header: https://core.trac.wordpress.org/ticket/42313
It turns out to be WordPress redirection
To prove it’s the WordPress that does the redirect, I tried the /a request on the other client WordPress sites. Those other sites gave me the same redirection.
Conclusion
I finally figured out that since WordPress 5.0.0, there’s a function that attempts to guess the correct URL for a 404 request. Here is the official developer documentation that mentions it: https://developer.wordpress.org/reference/functions/redirect_guess_404_permalink/.