2015-10-15 97 views
0

如何将* .appspot.com域名重定向到您的自定义域名。我要的是重定向这样的域:如何将* .appspot.com重定向到自定义域名

app-id.appspot.com -> mycustomdomain.com www.mycustomdomain.com -> mycustomdomain.com

注:我使用GO和大猩猩MUX。

+2

检查域中请求并重定向它,如果它不是你的规范。非常简单。 – thwd

+0

我是否必须执行所有我的处理函数? –

回答

3

您可以按here所述的方式执行http.Handler组合代码以重用代码。

在你的情况下,组合子会是这个样子(它调整自己的口味和要求):

func NewCanonicalDomainHandler(next http.HandlerFunc) http.HandlerFunc { 
    return func(w http.ResponseWriter, r *http.Request) { 

     if r.Host != "myapp.com" { 
      u := *r.URL 
      u.Host = "myapp.com" 
      u.Scheme = "http" 
      http.Redirect(w, r, u.String(), http.StatusMovedPermanently) 
      return 
     } 

     next(w, r) 

    } 
} 

的您可以与包装你的处理程序:

http.Handle("/foo", NewCanonicalDomainHandler(someHandler)) 
+0

我用'localhost'和'127.0.0.1'试过了。当我导航到'http:// localhost'时,它将重定向到'http:// localhost/127.0.0.1' –

+0

localhost是特殊的。但嘿,调整代码,玩弄它。 – thwd

+1

我终于明白了,我把'如果r.Url.Host'改为'if r.Host',然后加上'u.Scheme =“http”' –