首页 banner 站内链接自动补尾斜杠,避免多一次 301
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,9 @@ package playletser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"videoweb/app/service/upstream"
|
||||
@@ -101,7 +104,26 @@ func ActiveHomeBanners(list []HomeBannerData) []HomeBannerData {
|
||||
if !item.Status {
|
||||
continue
|
||||
}
|
||||
item.URL = normalizeBannerURL(item.URL)
|
||||
out = append(out, item)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// normalizeBannerURL 站内页面链接补尾斜杠,避免后台漏填 "/" 时每次点击都多一次 301。
|
||||
// 外链、协议相对地址、带扩展名的文件路径保持原样。
|
||||
func normalizeBannerURL(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if !strings.HasPrefix(raw, "/") || strings.HasPrefix(raw, "//") {
|
||||
return raw
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || u.Path == "" || strings.HasSuffix(u.Path, "/") {
|
||||
return raw
|
||||
}
|
||||
if strings.Contains(path.Base(u.Path), ".") {
|
||||
return raw
|
||||
}
|
||||
u.Path += "/"
|
||||
return u.String()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package playletser
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeBannerURL(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"/detail/hqtqxdqe": "/detail/hqtqxdqe/",
|
||||
"/detail/hqtqxdqe/": "/detail/hqtqxdqe/",
|
||||
" /play/abc ": "/play/abc/",
|
||||
"/detail/abc?from=banner": "/detail/abc/?from=banner",
|
||||
"/": "/",
|
||||
"": "",
|
||||
"https://example.com/a": "https://example.com/a",
|
||||
"//cdn.example.com/a": "//cdn.example.com/a",
|
||||
"/static/images/a.png": "/static/images/a.png",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := normalizeBannerURL(in); got != want {
|
||||
t.Errorf("normalizeBannerURL(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestActiveHomeBannersNormalizesURL(t *testing.T) {
|
||||
got := ActiveHomeBanners([]HomeBannerData{
|
||||
{ID: 1, URL: "/detail/abc", Status: true},
|
||||
{ID: 2, URL: "/detail/off", Status: false},
|
||||
})
|
||||
if len(got) != 1 || got[0].URL != "/detail/abc/" {
|
||||
t.Fatalf("unexpected banners: %+v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user