Skip to content

fix: prevent subdomain-only route from overwriting hostname route#10211

Open
jalexiscv wants to merge 1 commit into
codeigniter4:developfrom
jalexiscv:fix/bug-7214-hostname-subdomain-routing
Open

fix: prevent subdomain-only route from overwriting hostname route#10211
jalexiscv wants to merge 1 commit into
codeigniter4:developfrom
jalexiscv:fix/bug-7214-hostname-subdomain-routing

Conversation

@jalexiscv
Copy link
Copy Markdown

Description

Fixes #7214 - Inconsistent hostname/subdomain limitation in Routing

Problem

When using hostname and subdomain route options, the precedence was inconsistent depending on whether they were on the same route or separate routes:

Case 1: Same route (hostname took precedence - correct)

$routes->get('/', 'Home::index');
$routes->get('/', 'Media::index', ['hostname' => 'media.example.com', 'subdomain' => '*']);

media.example.com → Media::index ✓

Case 2: Separate routes (subdomain incorrectly won)

$routes->get('/', 'Home::index');
$routes->get('/', 'Media::index', ['hostname' => 'media.example.com']);
$routes->get('/', 'All::index', ['subdomain' => '*']);

media.example.com → All::index ✗ (should be Media::index)

Root Cause

Two issues:

  1. elseif meant routes with both hostname and subdomain only validated hostname
  2. No protection against subdomain-only routes overwriting hostname routes

Solution

  1. Change elseif to if — both hostname and subdomain are validated independently
  2. Track matchedByHostname/matchedBySubdomain flags during registration
  3. Add guard: routes matched only by subdomain cannot overwrite existing hostname routes

Changes

One file: system/Router/RouteCollection.php

  • Independent hostname/subdomain validation
  • Hostname route protection from subdomain-only overwrite

Ref: #7214
Closes #7214

Fixes codeigniter4#7214

When routes with both 'hostname' and 'subdomain' options are defined
separately, a less specific subdomain-only route could overwrite a
more specific hostname-matched route due to registration order.

Changes:
- Change 'elseif' to 'if' so both hostname and subdomain checks are
  independent. A route with both options now validates both.
- Track matchedByHostname/matchedBySubdomain flags during registration
- Add guard to prevent a route matched only by subdomain from
  overwriting an existing route that was registered with hostname,
  since hostname specificity should take precedence.

Ref: codeigniter4#7214
@mergeable
Copy link
Copy Markdown

mergeable Bot commented May 18, 2026

Hi there, jalexiscv! 👋

Thank you for sending this PR!

We expect the following in all Pull Requests (PRs).

Important

We expect all code changes or bug-fixes to be accompanied by one or more tests added to our test suite to prove the code works.

If pull requests do not comply with the above, they will likely be closed. Since we are a team of volunteers, we don't have any more time to work
on the framework than you do. Please make it as painless for your contributions to be included as possible.

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md

Sincerely, the mergeable bot 🤖

Copy link
Copy Markdown
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

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

Please add tests to prove the fix works. Also, add the fix to the changelog.

@paulbalandan paulbalandan added bug Verified issues on the current code behavior or pull requests that will fix them tests needed Pull requests that need tests docs needed Pull requests needing documentation write-ups and/or revisions. labels May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them docs needed Pull requests needing documentation write-ups and/or revisions. tests needed Pull requests that need tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Inconsistent hostname/subdomain limitation in Routing

2 participants