Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#44039 new defect (bug)

attachment rewrite rule redirects to posts instead of showing 404

Reported by: swedish-boy's profile 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
6 years 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
6 years 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 one and redirect I think. That's my point which I didn't clearly put. :)

Version 5, edited 6 years ago by swedish boy (previous) (next) (diff)
Note: See TracTickets for help on using tickets.