From 17a05e9e95ac399eea8f62e109c7f126e39f757e Mon Sep 17 00:00:00 2001 From: Ricardo Drizin Date: Thu, 10 Mar 2016 15:35:00 -0300 Subject: [PATCH] Looking for Post by Slug is now case insensitive. If case does not match, will do 301 redirect to the correct canonical url. # Conflicts: # database/retrieval.go --- database/retrieval.go | 2 +- templates/execution.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/database/retrieval.go b/database/retrieval.go index a5c20834..0126738d 100644 --- a/database/retrieval.go +++ b/database/retrieval.go @@ -15,7 +15,7 @@ const stmtRetrievePostsForApi = "SELECT id, uuid, title, slug, markdown, html, f const stmtRetrievePostsByUser = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, meta_description, image, author_id, published_at FROM posts WHERE page = 0 AND status = 'published' AND author_id = ? ORDER BY published_at DESC LIMIT ? OFFSET ?" const stmtRetrievePostsByTag = "SELECT posts.id, posts.uuid, posts.title, posts.slug, posts.markdown, posts.html, posts.featured, posts.page, posts.status, posts.meta_description, posts.image, posts.author_id, posts.published_at FROM posts, posts_tags WHERE posts_tags.post_id = posts.id AND posts_tags.tag_id = ? AND page = 0 AND status = 'published' ORDER BY posts.published_at DESC LIMIT ? OFFSET ?" const stmtRetrievePostById = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, meta_description, image, author_id, published_at FROM posts WHERE id = ?" -const stmtRetrievePostBySlug = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, meta_description, image, author_id, published_at FROM posts WHERE slug = ?" +const stmtRetrievePostBySlug = "SELECT id, uuid, title, slug, markdown, html, featured, page, status, meta_description, image, author_id, published_at FROM posts WHERE slug = ? COLLATE NOCASE" const stmtRetrieveUserById = "SELECT id, name, slug, email, image, cover, bio, website, location FROM users WHERE id = ?" const stmtRetrieveUserBySlug = "SELECT id, name, slug, email, image, cover, bio, website, location FROM users WHERE slug = ?" const stmtRetrieveUserByName = "SELECT id, name, slug, email, image, cover, bio, website, location FROM users WHERE name = ?" diff --git a/templates/execution.go b/templates/execution.go index b944ecad..3e3b1ae5 100644 --- a/templates/execution.go +++ b/templates/execution.go @@ -35,6 +35,9 @@ func ShowPostTemplate(writer http.ResponseWriter, r *http.Request, slug string) return err } else if !post.IsPublished { // Make sure the post is published before rendering it return errors.New("Post not published.") + } else if post.Slug != slug { + http.Redirect(writer, r, "/"+post.Slug+"/", 301) + return nil } requestData := structure.RequestData{Posts: make([]structure.Post, 1), Blog: methods.Blog, CurrentTemplate: 1, CurrentPath: r.URL.Path} // CurrentTemplate = post requestData.Posts[0] = *post