fix: 使用循环持续移除 URL 后缀

This commit is contained in:
lixiangwuxian 2025-04-16 00:44:01 +08:00
parent 20d82d485e
commit 0f0a2304cb

View File

@ -55,6 +55,10 @@ func normalizeURL(rawURL string) string {
// 将 http 和 https 视为同一种协议
u.Scheme = "https"
// 使用循环持续移除后缀直到Path不再变化
for {
oldPath := u.Path
// 移除尾部的 /index.html 或 .html
u.Path = strings.TrimSuffix(u.Path, "/index.html")
u.Path = strings.TrimSuffix(u.Path, ".html")
@ -62,6 +66,12 @@ func normalizeURL(rawURL string) string {
// 移除末尾的 /
u.Path = strings.TrimRight(u.Path, "/")
// 如果路径不再变化,则退出循环
if oldPath == u.Path {
break
}
}
return u.String()
}