Skip to content

Instantly share code, notes, and snippets.

@snluu
Created May 28, 2012 19:42
Show Gist options
  • Save snluu/2820865 to your computer and use it in GitHub Desktop.
Save snluu/2820865 to your computer and use it in GitHub Desktop.
Play Framework: Handle URL trailing slash with Application Global
import play.api._
import play.api.mvc._
object Global extends GlobalSettings {
override def onHandlerNotFound(request: RequestHeader): Result = {
// handle trailing slashes
if (request.path.endsWith("/")) {
// construct a new URI without the slash
// request.path doesn't contain query strings
// request.uri contains both the path and the query string
val uri = request.path.take(request.path.length - 1) + {
if (request.path == request.uri) "" // no query string
else request.uri.substring(request.path.length)
}
Results.MovedPermanently(uri)
}
else // not my business. push it back to super class
super.onHandlerNotFound(request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment