后台地址带尾斜杠直接 404(/admin/、/admin/login/、/account/) #2

Closed
opened 2026-11-23 17:33:55 +08:00 by DeepSeek · 0 comments
Collaborator

现象

公开页面带尾斜杠会 301 规范化,但后台路径带尾斜杠直接返回 404,且 404 页面是前台风格。

复现步骤

# 已登录状态下
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8787/admin/          # 404
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8787/admin/login/    # 404
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8787/admin/articles/ # 404
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8787/account/        # 404

# 对照:前台会正确 301
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' http://127.0.0.1:8787/articles/    # 301 → /articles
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' http://127.0.0.1:8787/cobalt-test/ # 301 → /cobalt-test

带尾斜杠访问 /admin/ 时,即使已经登录,拿到的也是前台「页面未找到」的 404 页面(<title>页面未找到</title>)。

期望

后台入口与前台一致:/admin/ → 301 /admin,/admin/login/ → 301 /admin/login,/account/ → 301 /account。

影响

  • 用户手输或从书签/文档里复制 https://站点/admin/ 打开后台,会看到 404 并以为后台没装好。
  • 部分反向代理或 CDN 规则会补尾斜杠,接入后后台入口直接不可用。
  • 404 内容属于前台模板,和当前所处位置不一致,容易误判。

原因定位

app/Controller/SiteController.php:218-235 的 missing() 只对两类路径做尾斜杠 301:

$known = $single && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published' && $normal !== '/home';
if ($known || preg_match('~^/articles(?:/[a-z0-9-]+){1,2}$~D', $normal)) {
    return View::redirect($normal . ($query !== '' ? '?' . $query : ''), 301);
}

/admin、/account 不在白名单里,于是落到 404。

修复建议

在 missing() 里对固定路由前缀(/admin、/admin/login、/account 等)一并放行 301;更稳妥的做法是在路由层统一做「去掉尾斜杠」的规范化重定向,favicon.ico 这类静态路径除外。

环境

  • 版本:main @ 01ede89(v1.1.0)
## 现象 公开页面带尾斜杠会 301 规范化,但后台路径带尾斜杠直接返回 404,且 404 页面是前台风格。 ## 复现步骤 ```bash # 已登录状态下 curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8787/admin/ # 404 curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8787/admin/login/ # 404 curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8787/admin/articles/ # 404 curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8787/account/ # 404 # 对照:前台会正确 301 curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' http://127.0.0.1:8787/articles/ # 301 → /articles curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' http://127.0.0.1:8787/cobalt-test/ # 301 → /cobalt-test ``` 带尾斜杠访问 `/admin/` 时,即使已经登录,拿到的也是前台「页面未找到」的 404 页面(`<title>页面未找到</title>`)。 ## 期望 后台入口与前台一致:`/admin/` → 301 `/admin`,`/admin/login/` → 301 `/admin/login`,`/account/` → 301 `/account`。 ## 影响 - 用户手输或从书签/文档里复制 `https://站点/admin/` 打开后台,会看到 404 并以为后台没装好。 - 部分反向代理或 CDN 规则会补尾斜杠,接入后后台入口直接不可用。 - 404 内容属于前台模板,和当前所处位置不一致,容易误判。 ## 原因定位 `app/Controller/SiteController.php:218-235` 的 `missing()` 只对两类路径做尾斜杠 301: ```php $known = $single && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published' && $normal !== '/home'; if ($known || preg_match('~^/articles(?:/[a-z0-9-]+){1,2}$~D', $normal)) { return View::redirect($normal . ($query !== '' ? '?' . $query : ''), 301); } ``` `/admin`、`/account` 不在白名单里,于是落到 404。 ## 修复建议 在 `missing()` 里对固定路由前缀(`/admin`、`/admin/login`、`/account` 等)一并放行 301;更稳妥的做法是在路由层统一做「去掉尾斜杠」的规范化重定向,`favicon.ico` 这类静态路径除外。 ## 环境 - 版本:`main` @ 01ede89(v1.1.0)
enoch closed this issue 2026-11-23 19:09:17 +08:00
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: laysense/laysite#2