Last active
August 29, 2015 14:23
-
-
Save karad/8f216c51e751e6f1ff12 to your computer and use it in GitHub Desktop.
Ring solution to redirect missing trailing slash urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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