From cf0a9e6a9ef61894a11c8ef3fb9f2d3c57e78b1d Mon Sep 17 00:00:00 2001 From: Dmitriy Zinin Date: Wed, 15 May 2019 22:55:25 +0300 Subject: [PATCH] Looking for Post by Slug is now case insensitive. If case doesn't match, 301 redirect to the correct canonical url. https://github.com/kabukky/journey/pull/82 --- 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 df4d21c8..56a4288b 100644 --- a/database/retrieval.go +++ b/database/retrieval.go @@ -16,7 +16,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 0540d038..d714c029 100644 --- a/templates/execution.go +++ b/templates/execution.go @@ -36,6 +36,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