goredir/main.go
Mans Ziesel a84875b684
All checks were successful
Build docker container / Build image (push) Successful in 32s
fix url
2024-09-06 19:54:15 +02:00

35 lines
861 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"))
fmt.Printf("Host: %s not found\n", r.Host)
return
}
// Replace "localhost" with "mziesel.nl" in the Host header
newHost := strings.Replace(r.Host, "mzsl.nl", "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)
}