How to redirect from others tlds to .com (www.example.org-> https://example.com)

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)

  1. 301 Redirect in .htaccess
  2. Host these tlds in one place and use the same dns lookup.

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.