Skip to content

Instantly share code, notes, and snippets.

@mbseid
Last active July 6, 2018 10:33
Show Gist options
  • Save mbseid/8327841 to your computer and use it in GitHub Desktop.
Save mbseid/8327841 to your computer and use it in GitHub Desktop.
Play Framework 2 HTTPS Redirect
override def onRouteRequest(req: RequestHeader): Option[Handler] = {
(req.method, req.headers.get("X-Forwarded-Proto")) match {
case ("GET", Some(protocol)) if protocol != "https" => Some(Action{ MovedPermanently("https://"+req.host+req.uri)})
case (_, _) => super.onRouteRequest(req)
}
}
@proton5000
Copy link

Play 2.3 provides a secure property in the request object.
I have updated your code for Play v 2.3.

override def onRouteRequest(req: RequestHeader): Option[Handler] = {
    (req.method, req.secure) match {
      case ("GET", false) if req.host.indexOf("127.0.0.1") == -1 || req.host.indexOf("localhost") == -1 =>
        Some(Action{ MovedPermanently("https://" + req.host + req.uri)})
      case (_, _) => super.onRouteRequest(req)
    }
  }

It automatically use auto redirect to https protocol on the server side, but locally work by http protocol for development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment