0
votes

I am using Google App Engine (go).. and I am looking to redirect https://www.ibikeride.com/scotland/comrie-croft-mountain-bike-trails/amp

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

I looked on google app engine docs on redirects but is more than pretty vague. I assume it is a redirect set up in the app.yaml file under handlers.

I actually want to redirect all files ending in "amp" to the same url structure without the amp if there is a straightforward way of doing that and to avoid me doing a 100+ individual redirect.

For relevant info here is how my current handlers look NB(before attempting this). I have a few handlers to remove the '.html' from the end of url's (for specific categories) and also one at the end to redirect all files to secure 'https'

Any help appreciated

 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
1

1 Answers

1
votes

Since you are using the classification part amp at the end instead of the beginning I would suggest you to create a small service that redirects automatically.

For instance in app.yaml:

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

And the code (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)
}