Heroku (PlayFramework/Scala) app automatically redirect to https

房东的猫 提交于 2020-01-06 07:25:38

问题


I have an PlayFramework App developed using Scala running on Heroku; I only mention the development language and framework because any posts I've found regarding this issue relate to PHP! I have http and https running on a custom domain but I would like to force http requests to be redirect to https.

I've found that I need to update the .htaccess file with the following:

##Force SSL 

#Normal way (in case you need to deploy to NON-heroku)
RewriteCond %{HTTPS} !=on

#Heroku way
RewriteCond %{HTTP:X-Forwarded-Proto} !https 

#If neither above conditions are met, redirect to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

But I am not sure if it is possible or how to set up the .htaccess file using Play and Scala.

Please can someone advise? Thanks.


回答1:


All you need to do is to add

play.filters.enabled += play.filters.https.RedirectHttpsFilter

In your .conf file.

It will redirect all HTTP requests to HTTPS automatically.

It only works in production mode by default. To change that, add :

play.filters.https.redirectEnabled = true

See the RedirectHttpsFilter documentation for more.



来源:https://stackoverflow.com/questions/50129048/heroku-playframework-scala-app-automatically-redirect-to-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!