【问题标题】:301 redirects for specific url's using Google App Engine(go)?使用Google App Engine(go)对特定网址进行301重定向?
【发布时间】:2020-03-21 13:27:47
【问题描述】:

我正在使用 Google App Engine (go).. 我希望重定向 https://www.ibikeride.com/scotland/comrie-croft-mountain-bike-trails/amp

https://www.ibikeride.com/scotland/comrie-croft-mountain-bike-trails

我查看了有关重定向的谷歌应用引擎文档,但内容非常模糊。我假设它是在处理程序下的 app.yaml 文件中设置的重定向。

我实际上想将所有以“amp”结尾的文件重定向到没有 amp 的相同 url 结构 如果有一种直接的方法可以避免我进行 100 多个单独的重定向。

这里的相关信息是我当前的处理程序看起来 NB(在尝试此之前)。我有一些处理程序可以从 url 的末尾删除“.html”(针对特定类别),最后还有一个处理程序可以将所有文件重定向到安全的“https”

任何帮助表示赞赏

 handlers:

# this serves your static/some/path/<file>.html asset as /some/path/<file>.html
- url: /(england/.*\.html)$
  static_files: static/\1
  upload: static/england/.*\.html$

# this serves your static/some/path/<file>.html asset as /some/path/<file>
- url: /(england/.*)$
  static_files: static/\1.html
  upload: static/england/.*\.html$

# this serves your static/some/path/<file>.html asset as /some/path/<file>.html
- url: /(scotland/.*\.html)$
  static_files: static/\1
  upload: static/scotland/.*\.html$

# this serves your static/some/path/<file>.html asset as /some/path/<file>
- url: /(scotland/.*)$
  static_files: static/\1.html
  upload: static/scotland/.*\.html$

# this serves your static/some/path/<file>.html asset as /some/path/<file>.html

- url: /(wales/.*\.html)$
  static_files: static/\1
  upload: static/wales/.*\.html$

# this serves your static/some/path/<file>.html asset as /some/path/<file>
- url: /(wales.*)$
  static_files: static/\1.html
  upload: static/wales/.*\.html$

  # this serves your static/some/path/<file>.html asset as /some/path/<file>.html
- url: /(northern-ireland/.*\.html)$
  static_files: static/\1
  upload: static/orthern-ireland/.*\.html$

# this serves your static/some/path/<file>.html asset as /some/path/<file>
- url: /(northern-ireland.*)$
  static_files: static/\1.html
  upload: static/northern-ireland/.*\.html$

#redirect always to https 
- url: /.*
  script: auto
  secure: always
  redirect_http_response_code: 301

【问题讨论】:

    标签: google-app-engine redirect http-status-code-301


    【解决方案1】:

    由于您在末尾而不是开头使用分类部分amp,我建议您创建一个自动重定向的小型服务。

    例如在 app.yaml 中:

    handlers:
    ....
    - url: /.*/amp
      script: main.go
    

    以及代码(main.go):

    package main
    
    import (
        "fmt"
        "net/http"
    )
    
    type Handler struct{}
    
    
    func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
        uri := r.URL.Path
        length := len(uri)
        newUrl := uri[0:length-3] // Remove trailing amp
        http.Redirect(w, r, newUrl, http.StatusMovedPermanently)
        return
    }
    
    func main() {
        handler := new(Handler)
        http.ListenAndServe(":8080", handler)
    }
    

    【讨论】:

    • 感谢您的直观。我正在使用 go112 并收到错误... INVALID_ARGUMENT:处理程序“/.*/amp”的脚本字段必须设置为运行时 go112 的“自动”。您将如何为此定制?
    猜你喜欢
    • 2012-09-22
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 2015-03-05
    相关资源
    最近更新 更多