WordPress.org

Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#44039 new defect (bug)

attachment rewrite rule redirects to posts instead of showing 404

Reported by: swedish boy Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version:
Component: Rewrite Rules Keywords:
Focuses: Cc:

Description

Instead of showing 404 for an invalid attachment slug, you will be redirected to a valid post slug.

The blog is set to permalink structure %postname%

The attachment rule for this is [^/]+/([^/]+)/?$

And the rewrite is index.php?attachment=$matches[1]

Tested on a clean WordPress-installation running twentyseventeen if you visit /hello-world/sample-page/ you will be redirected to /sample-page.

Is this really how it should be?

Change History (2)

#1 in reply to: ↑ description @SergeyBiryukov
7 weeks ago

Hi @swedish-boy, welcome to WordPress Trac! Thanks for the report.

Tested on a clean WordPress-installation running twentyseventeen if you visit /hello-world/sample-page/ you will be redirected to /sample-page.

Is this really how it should be?

That's WordPress trying to guess the correct URL instead of showing the 404 error, which may or may not be the expected result, depending on the use case. Seeredirect_guess_404_permalink().

Related: #12601.

#2 @swedish boy
7 weeks ago

Ok. Any filter or hook to disable redirect_guess_404_permalink?

I fixed this issue myself by removing attachment rewrites.

(I'm sorry that my example did not use an attachment, but the rewrite rules that's the problem is the same [^/]+/([^/]+)/?$)

If the first part [^/]+ would match to a post the second should never be matched to another thing unless it's a valid hit for an attachment in this case. That's my point which I didn't clearly put. :)

Last edited 7 weeks ago by swedish boy (previous) (diff)
Note: See TracTickets for help on using tickets.