1

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?

2
  • Yes, this is WordPress trying to be helpful by redirecting bad URLs to a close match. Do you want to turn that off? I can't remember what it's called in the code though to search for it right now. – Rup Oct 26 '20 at 9:51
  • Rup, yes i want to turn it off. My SEO specialist says that it has a bad effect on website promotion – Yushko Aleksandr Oct 26 '20 at 9:54
2

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.

2
  • Thanks for the answer. I didn't notice that I was getting rel="canonical" on the returned page. – Yushko Aleksandr Oct 26 '20 at 10:19
  • There's a filter somewhere that turns this off, but I wouldn't recommend it – Tom J Nowell Oct 26 '20 at 10:31
0

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' );

Link to article

1
  • I don't think that was the problem here though? (although I admit I thought it was to start with) – Rup Dec 16 '20 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.