Hosting on App Engine and handling alias redirects

Hey everyone,

I’m migrating from self-hosted WordPress to Hugo on Google App Engine, and I currently have a small Go script to launch the generated pages from /public/ which works fine.

However, while I’m migrating my posts, I’m renaming several of the URLs, and so I’ve been adding ‘aliases’ entries to my BlackFriday headers. However, since Hugo’s internal web server isn’t what’s running on App Engine, I totally understand that the best Hugo can do is generate a meta-refresh redirect with a 0-second timeout, which search engines will generally be okay with. This redirect method, though, may not give the best SEO and may not change search engine entries to the new URL.

I’m curious if anyone wants to collaborate on a new feature for Hugo to use a flag in config.toml to generate a list of 301 redirects based on aliases, and output that to something like an .htaccess file within the /public/ folder. I could, for my own purposes, parse that list and regenerate my main.go launcher.

My launcher script currently looks like this:

package main
import "net/http"
func init() {
	http.Handle("/", http.FileServer(http.Dir("public")))
}

I’m wondering if building up a redirect function like this would help:

func redirect(w http.ResponseWriter, r *http.Request, newUrl string) {
	http.Redirect(w, r, newUrl, 301)
}

and then above the http.Handle() call in my init() method, maybe we could repeat something like this over and over?

http.HandleFunc("/old-url", func(w http.ResponseWriter, r *http.Request) {
	redirect(w, r, "http://example.com/new-url")
})

UPDATE: this method works. Now I just need to get Hugo to build a file out of the aliases if config.toml says to generate .htaccess or something. Any takers?

New launcher script looks like this:

package main
import "net/http"

func redirect(w http.ResponseWriter, r *http.Request, newUrl string) {
	http.Redirect(w, r, newUrl, 301)
}

func init() {
	http.HandleFunc("/old-url", func(w http.ResponseWriter, r *http.Request) {
		redirect(w, r, "http://my_base_url.com/new-url")
	})

	http.Handle("/", http.FileServer(http.Dir("public")))
}

I’m not convinced this a feature worth the implementation, maintainance and support cost. .htaccess is Apache format, and adding such support will soon get the Nginx and the Caddy etc. crowd shouting.

My advice would be to jump in on this:

The alias entries are available – and I can imagine a “htaccess” template definition would fit into my thoughts outlined in the thread above.

HIi Bep, thanks for the reply.

.htaccess was just a suggestion, I’d be just as happy with any two-column format, heck even a CSV of “old_url,new_url”. I think that Hugo’s code would still need to be tweaked in order to scrape all of the aliases out of the files.

Then again, I suppose I could just whip up a Python or Go script to walk through my /content/ folder, look for any files with “aliases” in the BlackFriday config (would have to account for all three methods), and then just write my main.go script on the fly.

I’ll check out that other thread, thanks for sending it along.