Skip to content

Instantly share code, notes, and snippets.

@karad
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karad/8f216c51e751e6f1ff12 to your computer and use it in GitHub Desktop.
Save karad/8f216c51e751e6f1ff12 to your computer and use it in GitHub Desktop.
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