goredir/main.go
Mans Ziesel 7780311ea6
All checks were successful
Build docker container / Build image (push) Successful in 1m4s
first commit
2024-09-06 19:45:32 +02:00

34 lines
818 B
Go

package main
import (
"fmt"
"net/http"
"strings"
)
func greet(w http.ResponseWriter, r *http.Request) {
// Check if the Host header ends with "mzsl.nl"
if !strings.HasSuffix(r.Host, "mzsl.nl") {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404, url not found"))
return
}
// Replace "localhost" with "mziesel.nl" in the Host header
newHost := strings.Replace(r.Host, "localhost", "mziesel.nl", 1)
// Construct the new URL for redirection
newUrl := "https://" + newHost + r.URL.String()
fmt.Println(newUrl)
// Set the Location header and respond with a 301 Moved Permanently status
w.Header().Set("Location", newUrl)
w.WriteHeader(http.StatusMovedPermanently)
w.Write([]byte("url found, redirecting..."))
}
func main() {
http.HandleFunc("/", greet)
http.ListenAndServe(":8080", nil)
}