Skip to content

Return a 404 when filtering the blog by a non-existent tag - #1672

Open
Amoifr wants to merge 1 commit into
symfony:mainfrom
Amoifr:fix-1454-tag-not-found-404
Open

Return a 404 when filtering the blog by a non-existent tag#1672
Amoifr wants to merge 1 commit into
symfony:mainfrom
Amoifr:fix-1454-tag-not-found-404

Conversation

@Amoifr

@Amoifr Amoifr commented Jul 23, 2026

Copy link
Copy Markdown

Fixes #1454.

On the blog index, requesting a tag that does not exist (/blog/?tag=this-tag-does-not-exist) silently lists every latest post instead of telling the user the tag is unknown, because findOneBy() returns null and findLatest() then applies no tag filter.

As @GromNaN suggested on the issue, this returns a 404 when a tag is requested but not found, which is both more helpful for users and the expected behavior for SEO. Requesting an existing tag, or no tag at all, is unchanged.

Added two functional tests to BlogControllerTest: one for an existing tag (still 200) and one for an unknown tag (now 404).

Thanks @mindaugasvcs for the report and @GromNaN for the direction! 🙏

Comment thread src/Controller/BlogController.php Outdated
@@ -55,6 +55,12 @@ public function index(Request $request, int $page, string $_format, PostReposito

if ($request->query->has('tag')) {
$tag = $tags->findOneBy(['name' => $request->query->get('tag')]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial ticket suggested removing this unnecessary DB query. The tag slug can be used to find articles directly; then return the 404 when no articles are found.

@Amoifr
Amoifr force-pushed the fix-1454-tag-not-found-404 branch from 25dc49a to da76be9 Compare July 23, 2026 12:27
@Amoifr

Amoifr commented Jul 23, 2026

Copy link
Copy Markdown
Author

Good point, thanks! I reworked the PR accordingly: the Tag lookup is gone, findLatest() now filters directly on the tag name (through a dedicated join so posts keep all their tags), and the controller returns a 404 when no articles are found for the requested tag. The existing tests cover both cases and the full suite is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove unnecessary SQL queries

2 participants