Non-existent page returns code 200
-
21-04-2021 - |
Domanda
I have a page like mysite.com/contacts/ If I enter non-existent urls like:
mysite.com/contacts--/
mysite.com/contacts%20/
i get status 200 and the page opens mysite.com/contacts/ although if I enter:
mysite.com/contacts1/
As expected, I am getting a 404 error.
How can I implement error handling at the CMS level so that non-existent pages return a page with a 404 error template? There may be some plugin that implements URL management?
Soluzione
That's not what's happening, when you visit /contacts--/
it doesn't return a 200 code, but instead it returns a 301
redirect code. The browser then follows this redirect and heads to /contact
and it's /contact
that returns 200
code.
This is because /contact
is the canonical URL of that page, and WordPress redirects to canonical pages out of the box for improved SEO. It should also be adding a meta tag to the header containing the canonical URL to avoid duplicate content penalties.
Altri suggerimenti
If you need to prevent WordPress from guessing URLs, you can add the following code to function.php
function remove_redirect_guess_404_permalink( $redirect_url ) {
if ( is_404() ) {
return false;
} //end if
return $redirect_url;
} //end remove_redirect_guess_404_permalink()
add_filter( 'redirect_canonical', 'remove_redirect_guess_404_permalink' );