diff options
| author | Russ Cox <rsc@golang.org> | 2024-12-10 11:33:47 -0500 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2024-12-10 17:57:17 +0000 |
| commit | df9a971e20b8cc0a3f08a172ddbb0ded29b3fc4e (patch) | |
| tree | e0a60795fc55a8f764bc7765525ae11a6b083391 | |
| parent | 8e64cd8e6c80f46530057b6ae7b2543343ec15fb (diff) | |
| download | go-x-website-df9a971e20b8cc0a3f08a172ddbb0ded29b3fc4e.tar.xz | |
cmd/golangorg: redirect various dynamic content on tip.golang.org
This makes links in package docs work better.
Fixes golang/go#58484.
Change-Id: Ie399bf94262b0aaeb5eed7c0ef18a2a938e9ef7c
Reviewed-on: https://go-review.googlesource.com/c/website/+/634915
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
| -rw-r--r-- | cmd/golangorg/server.go | 34 | ||||
| -rw-r--r-- | cmd/golangorg/testdata/web.txt | 15 |
2 files changed, 44 insertions, 5 deletions
diff --git a/cmd/golangorg/server.go b/cmd/golangorg/server.go index 80883edc..9f672aee 100644 --- a/cmd/golangorg/server.go +++ b/cmd/golangorg/server.go @@ -223,9 +223,22 @@ func NewHandler(contentDir, goroot string) http.Handler { // which broke the redirect. mux.Handle("m.golang.org/", http.RedirectHandler("https://mail.google.com/a/golang.org/", http.StatusMovedPermanently)) - // Register a redirect handler for tip.golang.org/dl/ to the golang.org download page. - // (golang.org/dl and golang.google.cn/dl are registered separately.) - mux.Handle("tip.golang.org/dl/", http.RedirectHandler("https://go.dev/dl/", http.StatusFound)) + // Redirect synthetic subtrees on tip.golang.org over to go.dev. + tipRedirects := []string{ + "blog", + "change", + "cl", + "design", + "dl", + "s", + "talks", + "tour", + } + for _, name := range tipRedirects { + mux.Handle("tip.golang.org/"+name+"/", redirectPrefix("https://go.dev/")) + } + // tip.golang.org/play redirect adds ?v=gotip so the playground defaults to tip. + mux.Handle("tip.golang.org/play/", redirectPrefixQuery("https://go.dev/", "v=gotip")) // TODO(rsc): The unionFS is a hack until we move the files in a followup CL. siteMux := http.NewServeMux() @@ -876,10 +889,21 @@ func (a *atomicFS) Open(name string) (fs.File, error) { } func redirectPrefix(prefix string) http.Handler { + return redirectPrefixQuery(prefix, "") +} + +func redirectPrefixQuery(prefix, query string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { url := strings.TrimSuffix(prefix, "/") + "/" + strings.TrimPrefix(r.URL.Path, "/") - if r.URL.RawQuery != "" { - url += "?" + r.URL.RawQuery + if r.URL.RawQuery != "" || query != "" { + url += "?" + if r.URL.RawQuery != "" { + url += r.URL.RawQuery + if query != "" { + url += "&" + } + } + url += query } http.Redirect(w, r, url, http.StatusMovedPermanently) }) diff --git a/cmd/golangorg/testdata/web.txt b/cmd/golangorg/testdata/web.txt index da95c428..31a0d6b6 100644 --- a/cmd/golangorg/testdata/web.txt +++ b/cmd/golangorg/testdata/web.txt @@ -193,6 +193,21 @@ redirect == https://pkg.go.dev/std@master GET https://tip.golang.org/dl/ redirect == https://go.dev/dl/ +GET https://tip.golang.org/change/123 +redirect == https://go.dev/change/123 + +GET https://tip.golang.org/cl/123 +redirect == https://go.dev/cl/123 + +GET https://tip.golang.org/s/123 +redirect == https://go.dev/s/123 + +GET https://tip.golang.org/play/ +redirect == https://go.dev/play/?v=gotip + +GET https://tip.golang.org/play/p/123 +redirect == https://go.dev/play/p/123?v=gotip + GET https://golang.org/pkg?m=old redirect == https://go.dev/pkg?m=old |
