Tosin Davies-Tuase
Tosin Davies-Tuase

Reputation: 11

How do I implement vanity url in my play application?

I would like for each user on my app to have a dedicated vanity url in the format http://user1.example.com. How can I implement this in Play Application. Thank you.

Upvotes: 1

Views: 284

Answers (3)

Sameer Segal
Sameer Segal

Reputation: 22023

I was getting null values and the domain name kept changing to localhost:9000 until I solved it using ProxyPreserveHost on in the apache configuration (httpd.conf) file

Upvotes: 0

skrii
skrii

Reputation: 412

In a controller method, you can get the domain name from request.domain variable. You can then parse the user name or whatever identifier you want to use from the domain name using something like:

int endIndex = request.domain.indexOf(".example.com");
if (endIndex <= 0) {
  return null;
}
String subdomain = request.domain.substring(0, endIndex);

If you plan to use this subdomain check in several methods, you can make a separate domain check method that has @play.mvc.Before annotation and put the user object to renderArgs for later use in the actual invoked controller method and template:

@Before
public static void resolveUserSubdomain() {
   ... Check subdomain and find user ...
   renderArgs.put("user", user);       
}

You can also add this domain checker to a separate controller class and include it in every controller where you need it by using @play.mvc.With annotation for the class.

@With({ExampleDomainCheck.class})
public class ExampleController extends Controller {
    ... Controller methods ...
}

Upvotes: 2

Pere Villega
Pere Villega

Reputation: 16439

You can use virtual hosts. From release notes on Play 1.1:

The routes file now supports Host matching. this can be useful if action parameters must be extracted from the host parameter. For example, for a SAAS application, you could use:

GET    {client}.mysoftware.com/         Application.index

and then automatically retrieve the client value as for any other request parameter:

public static void index(String client) {
    ...
}

That should do it.

Upvotes: 2

Related Questions