最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp redirect - Why is WordPress redircting to posts based on slug

programmeradmin6浏览0评论

We have a WordPress site with a permalink structure like this:

news/%year%/%monthnum%/%day%/%postname%/

I noticed today that if I attempt to visit a non-exist page, WordPress will seemingly grab the first (?) post that is a partial match for that slug.

For example:

real: /news/2025/02/15/story-name

is a valid post page. But if the user attempts to go to

not real: /some/page/that/does/not/exist/story-name

It will resolve to the "real page" because the slug matches. Whats more, this will actually happen for a partial match like this:

not real: /some/page/that/does/not/exist/story

This is not the behavior I want. I noticed it because I was trying to use the template_redirect action to catch and redirect some legacy URLs. One of those legacy URLs happens to end with story so this re-direction was happening. It seemed to happen before the template_redirect action.

What is going on here and how do I make WordPress bend to my will?

We have a WordPress site with a permalink structure like this:

news/%year%/%monthnum%/%day%/%postname%/

I noticed today that if I attempt to visit a non-exist page, WordPress will seemingly grab the first (?) post that is a partial match for that slug.

For example:

real: /news/2025/02/15/story-name

is a valid post page. But if the user attempts to go to

not real: /some/page/that/does/not/exist/story-name

It will resolve to the "real page" because the slug matches. Whats more, this will actually happen for a partial match like this:

not real: /some/page/that/does/not/exist/story

This is not the behavior I want. I noticed it because I was trying to use the template_redirect action to catch and redirect some legacy URLs. One of those legacy URLs happens to end with story so this re-direction was happening. It seemed to happen before the template_redirect action.

What is going on here and how do I make WordPress bend to my will?

Share Improve this question asked Mar 18 at 20:23 ScribblemacherScribblemacher 1557 bronze badges 3
  • Does this answer help you? – Pat J Commented Mar 19 at 2:23
  • 2 @PatJ no. I found this is being caused by redirect_guess_404_permalink(), which I added as an answer for posterity (and/or for myself when I forget about this in 6 months). – Scribblemacher Commented Mar 19 at 18:22
  • Ah, good. Once you're able to—after 36 or maybe 48 hours—mark your answer as Accepted, which may help others who have the same question in the future. – Pat J Commented Mar 20 at 1:57
Add a comment  | 

1 Answer 1

Reset to default 1

Looks like this was being caused by the redirect_guess_404_permalink() function. To disable this behavior, I used:

add_filter( 'do_redirect_guess_404_permalink', '__return_false' );
发布评论

评论列表(0)

  1. 暂无评论