From d84d70769154a47449deb0ec3c003301d1d21958 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 26 May 2021 15:34:00 -0400 Subject: go.dev/cmd/frontend: fix learn.go.dev by redirecting to go.dev/learn The old Hugo setup was strange in that Hugo thought it was generating pages for go.dev/learn, and then we put in a redirect from go.dev/learn to learn.go.dev, and then we had a whole separate app just for serving files in that subtree. All the actual file serving was done in app.yaml using static file directives, not anything in Go. In the migration to a Go web server, I did not make learn.go.dev/ work, so it was showing the main go.dev page instead, which is clearly bad. I spent a little while trying to make the Go server behave like the old setup, but in the end I gave up and removed all the subterfuge. Now the pages are served from go.dev/learn, same as in the _content tree, and there is a redirect from learn.go.dev/anything to go.dev/learn/anything. Given how much trouble I had getting the server to work the other way, it seemed like we'd keep tripping over this special case as we make more changes. And given that the long-term plan is to move everything onto go.dev, it seemed better all around to just start telling the truth now. This fix is already deployed, to fix the broken learn.go.dev. Change-Id: I774850ade327623eefffc785f923f5002f3abff8 Reviewed-on: https://go-review.googlesource.com/c/website/+/322970 Trust: Russ Cox Run-TryBot: Russ Cox Reviewed-by: Jamal Carvalho --- go.dev/cmd/frontend/main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/go.dev/cmd/frontend/main.go b/go.dev/cmd/frontend/main.go index 93572e43..3d9e0a40 100644 --- a/go.dev/cmd/frontend/main.go +++ b/go.dev/cmd/frontend/main.go @@ -10,6 +10,7 @@ import ( "net/http" "net/url" "os" + "strings" "golang.org/x/go.dev/cmd/internal/site" ) @@ -32,7 +33,7 @@ func main() { } http.Handle("/", addCSP(http.FileServer(godev))) http.Handle("/explore/", http.StripPrefix("/explore/", redirectHosts(discoveryHosts))) - http.Handle("/learn/", http.StripPrefix("/learn/", redirectHosts(map[string]string{"": "learn.go.dev"}))) + http.Handle("learn.go.dev/", http.HandlerFunc(redirectLearn)) addr := ":" + listenPort() if addr == ":0" { @@ -47,6 +48,10 @@ func main() { log.Print(http.Serve(l, nil)) } +func redirectLearn(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, "https://go.dev/learn/"+strings.TrimPrefix(r.URL.Path, "/"), http.StatusMovedPermanently) +} + func listenPort() string { if p := os.Getenv("PORT"); p != "" { return p -- cgit v1.3