Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ring solution to redirect missing trailing slash urls
;;; http://stackoverflow.com/questions/14507445/ring-solution-to-redirect-missing-trailing-slash-urls
(defn good-response? [resp]
(and resp (not= (:status resp) 404)))
(defn wrap-slash [handler]
(fn [{:keys [uri] :as req}]
(let [resp (handler req)]
(if (or (good-response? resp) (.endsWith uri "/"))
resp
(let [added-slash (str uri "/")]
(if (good-response? (handler (-> req
(assoc :uri added-slash)
(assoc :path-info added-slash))))
(redirect added-slash)
resp))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment