Created
May 28, 2012 19:42
-
-
Save snluu/2820865 to your computer and use it in GitHub Desktop.
Play Framework: Handle URL trailing slash with Application Global
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
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