Skip to content

サブサイトに存在しない URL を、メインサイトの同じ URL へリダイレクトしない問題を修正 - #4656

Merged
ryuring merged 2 commits into
5.4.xfrom
fix/redirect-main-site-middleware
Nov 22, 2026
Merged

ryuring merged 2 commits into
5.4.xfrom
fix/redirect-main-site-middleware

Conversation

@ryuring

@ryuring ryuring commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

概要

サブサイトにコンテンツが存在しない URL にアクセスしたとき、メインサイトの同じ URL にコンテンツがあればメインサイトへリダイレクトする機能(4系の BcRedirectMainSiteFilter)が動作していない問題を修正します。

再現手順

  1. エイリアス en のサブサイトを作り、メインサイトにだけ /about の固定ページを作る
  2. /en/about にアクセスする
  3. 4系では /about へ 302 でリダイレクトされるが、5系では 404 になる

原因

  • BcRedirectMainSiteMiddleware がミドルウェアに登録されていない
  • また、ミドルウェア自体もコンテンツの有無を判定しておらず、登録するとサブサイトのすべての URL をメインサイトへリダイレクトしてしまう作りだった

修正

  • BcRedirectMainSiteMiddleware を 4系の判定に揃えて作り直した
    • 後続の処理で 404(NotFoundException・MissingRouteException・MissingControllerException)になった場合だけ判定する
    • サブサイトの URL がコンテンツ・コントローラーに該当する場合(記事が存在しないなどの 404)はリダイレクトしない
    • メインサイトの同じ URL がコンテンツ・コントローラーに該当する場合だけ 302 でリダイレクトする
    • GET / HEAD 以外、管理画面、未インストール時は対象外。別ホストへのリダイレクト(//example.com 等)にならないようにした
  • BaserCorePlugin::middleware() で、RoutingMiddleware の直前に登録した(ルーティングで発生する例外も受け取るため)

テスト

  • BcRedirectMainSiteMiddlewareTest を新しい仕様で書き直した(リダイレクトする/メインサイトにも無い/サブサイトにコンテンツがある/404 にならない の 4 ケース)
  • BaserCorePluginTest::testMiddleware で登録位置を確認
  • plugins/baser-core/tests/TestCase/Middleware・PagesControllerTest・BcErrorControllerTest・BlogControllerTest が通ることを確認

🤖 Generated with Claude Code

BcRedirectMainSiteMiddleware がミドルウェアに登録されておらず、
またコンテンツの有無を判定せずにサブサイトの URL をすべてリダイレクトする実装だったため、
4系の BcRedirectMainSiteFilter と同じ判定に揃えて登録する

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 06:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The redirect drops the original query string, so requests such as /en/news/?page=2 lose pagination state and display the first page instead of the requested page.

1 open issue
What changed in this PR

サブサイトに存在しない URL へのアクセスを、メインサイトの対応する URL へ転送する機能を修正する PR です。

変更内容:

  • 対象の 404 例外を受けた場合に、サブサイトとメインサイトのルーティング可否を確認して 302 リダイレクトするよう変更。
  • ミドルウェアを RoutingMiddleware の直前に登録。
  • リダイレクト条件とミドルウェアの登録位置を確認するテストを更新。
File Description
plugins/​baser-core/​tests/​TestCase/​Middleware/​BcRedirectMainSiteMiddlewareTest.php 転送する場合・しない場合の4ケースを検証。
plugins/​baser-core/​tests/​TestCase/​BaserCorePluginTest.php ミドルウェアの件数と登録位置を確認。
plugins/​baser-core/​src/​Middleware/​BcRedirectMainSiteMiddleware.php 404 時の転送条件と転送先の安全性チェックを実装。
plugins/​baser-core/​src/​BaserCorePlugin.php メインサイト転送用ミドルウェアを登録。

Review effort: Balanced

return $response->withLocation($request->getAttribute('base') . $mainSiteUrl);
if (!$this->isRoutable($request->withUri($request->getUri()->withPath($mainSiteUrl)))) return null;
return (new Response(['status' => 302]))
->withLocation($request->getAttribute('base') . $mainSiteUrl);
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ryuring
ryuring merged commit efef97d into 5.4.x Nov 22, 2026
5 checks passed
@ryuring
ryuring deleted the fix/redirect-main-site-middleware branch November 22, 2026 06:59
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.

4 participants