I want to point multiple subdomains and/or root domains to a single Play Framework 2.3 (Scala) application, for example apples.com, bananas.com or buy.bananas.com.
Depending on which domain the request comes in, I want to have different routes.
Ideally, it should work something like this:
GET apples.com @controllers.ApplesController.home
GET bananas.com @controllers.BananasController.home
GET buy.bananas.com @controllers.BananasController.buy
Is there any way to do this in Play Framework 2.3?
I am working in java .Here is the way to do it in java maybe that could help
routes
GET / @controllers.ApplesController.index
GET /apples @controllers.ApplesController.home
GET /bananas @controllers.BananasController.home
GET /buybananas @controllers.BananasController.buy
controller
@With(CheckUrl.class)
public static Result index() {
return ok(index.render("Unable to resolve host."));
}
CheckUrl.java
public class CheckUrl extends play.mvc.Action.Simple {
public F.Promise<SimpleResult> call(Http.Context ctx) throws Throwable {
String host = request().host();
System.out.println("HOST IS "+host);
if (host.equalsIgnoreCase("apples.com")) {
return F.Promise.pure(redirect("/apples"));
}else if (host.equalsIgnoreCase("bananas.com ")){
return F.Promise.pure(redirect("/bananas"));
}else if (host.equalsIgnoreCase("buy.bananas.com")){
return F.Promise.pure(redirect("/buybananas"));
}else{
return delegate.call(ctx);
}
}
I dont know if it the good way of doing it.I have tried it with request().uri()
but not with request().host()
and that worked for me.Maybe that could help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With