Hi, im doing a web server and i have lots of tlds, .es .uk .asia .it .cc .org
I need to redirect everything even www.example.com to https://example.com
(Im using Autocert for Lets Encrypt https certificates)
Hi, im doing a web server and i have lots of tlds, .es .uk .asia .it .cc .org
I need to redirect everything even www.example.com to https://example.com
(Im using Autocert for Lets Encrypt https certificates)
We use both methods.
Hi
A simple way of doing it in go is to use somekind of middleware function which checks if the requested host is correct and otherwise redirects to the correct host:
package main
import (
"io"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world")
}
func redirectOthers(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Host != "local.test.com" {
http.Redirect(w, r, "http://local.test.com", 301)
return
}
next.ServeHTTP(w, r)
}
}
func main() {
http.HandleFunc("/", redirectOthers(hello))
http.ListenAndServe(":80", nil)
}
to test this put this line in your host file
127.0.0.1 localhost local.test.org local.test.se local.test.com
And if you try http://local.test.se or http://local.test.org will it be redirected to http://local.test.com.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.