【问题标题】:How to redirect *.appspot.com to custom domain如何将 *.appspot.com 重定向到自定义域
【发布时间】:2015-10-15 09:57:48
【问题描述】:

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

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

注意:我正在使用 go 和 gorilla mux。

【问题讨论】:

  • 检查请求中的域,如果它不是您的规范域,则将其重定向。非常简单。
  • 我的所有处理函数都必须这样做吗?

标签: google-app-engine go


【解决方案1】:

您可以按照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))

【讨论】:

  • 我用localhost127.0.0.1 试过这个。当我导航到http://localhost 时,它将重定向到http://localhost/127.0.0.1
  • localhost 是特殊的。但是,嘿,调整代码,玩弄它。
  • 终于搞定了,我把if r.Url.Host改成if r.Host然后加上u.Scheme = "http"
猜你喜欢
  • 2016-05-19
  • 2013-11-05
  • 2015-05-12
  • 2019-02-14
  • 2015-03-11
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
相关资源
最近更新 更多