Community Virtual Hosting allows a community in a portal instance to be identified by a unique host name (DNS entry). Let’s say you have a portal instance running on a server called “NBA” and it has 2 communities called “Lakers” and “Celtics”. The “Lakers” community has a friendly url of “/lakers” and the “Celtics” community has a friendly url of “/celtics”. Public pages for the “Lakers” are accessed at http://NBA/web/lakers and the public pages for the “Celtics” are accessed at http://NBA/web/celtics. We assign a public virtual host of “lakers.nba.com” to the “Lakers” community and assign a public virtual host of “celtics.nba.com” to the “Celtics” community. We can now use http://lakers.nba.com to access the public pages of the “Lakers” community and http://celtics.nba.com to access the public pages of the “Celtics” community. All of this works out of the box with Liferay Portal.

Another url that will display the public pages for the “Lakers” community is http://celtics.nba.com/web/lakers. Another url that will display the public pages for the “Celtics” community is http://lakers.nba.com/web/celtics. Some people may be fine with these urls. Others would NOT be :). Luckily it’s a fairly simple fix to restrict a virtual host to only display specific communities.

We want to add the following to  com.liferay.portal.servlet.filters.virtualhost.VirtualHostFilter.processFilter():

if (layoutSet != null && !friendlyUrlAllowed(friendlyURL, layoutSet)) {
Group group = layoutSet.getGroup();
StringBundler forwardURL = new StringBundler();
forwardURL.append(_PUBLIC_GROUP_SERVLET_MAPPING + group.getFriendlyURL());
RequestDispatcher requestDispatcher =
_servletContext.getRequestDispatcher(forwardURL.toString());
requestDispatcher.forward(request, response);
return;
}

The business rules for which communities are allowed to be displayed per virtual host would be included in friendlyUrlAllowed():

private boolean friendlyUrlAllowed(String friendlyURL, LayoutSet layoutSet) {
try {
if (friendlyURL.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING + StringPool.SLASH) || friendlyURL.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING + StringPool.SLASH)) {
Group group = layoutSet.getGroup();
if (!friendlyURL.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING + group.getFriendlyURL()) && !friendlyURL.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING + group.getFriendlyURL()) && !friendlyURL.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING + "/control_panel")) {
return false;
}
}
}
catch (Exception e) {
_log.error(e, e);
}
return true;
      }

The end result is you will never see a Celtics page under http://lakers.nba.com!

Share This