« Back

Lose ten pounds off your URL

Company Blogs July 30, 2009 By Jonathan Neal Staff

UPDATE: I've added a 1 minute demonstration of this entire tutorial at the bottom of this blog.

There's a lot under the hood when it comes to Liferay, and even that is an understatement, but in my opinion there's one very useful feature that never gets enough play; virtual hosting.  In Liferay, virtual hosting is a free, out-of-the-box feature used to host more than one domain name on the same IP address, like running www.a.com, www.b.com, and www.c.com all running on one copy of Liferay.  Cool, right?  So, let me show you how to set up virtual hosting on your machine, because it should only take 5 minutes.

First, setup your machine to force some domain names redirect to your local copy of Liferay.  This step involves editing the hosts file on your machine, and it is only for testing purposes, so it won't actually change the way anyone else in the world sees these domains.

To do this in Windows, go to the %WINDIR%\System32\drivers\etc directory and open the hosts file in Notepad.  To do this in Mac OS, go to the /etc directory and open the hosts file in TextEdit.  If you're running Linux, well, you should already know how to do this.  Once the file is opened, add the following entry on a new line:       www.alpha.com

Next, startup Liferay and test the new domain.  Visiting http://localhost:8080/ should redirect you to http://localhost:8080/web/guest/ and you should see your local copy of Liferay.

Much the same, visiting http://www.alpha.com:8080/ should redirect you to http://www.alpha.com:8080/web/guest/, just like localhost.

Now it's time to map a new community to www.alpha.com.  The first step is to add a new community, so go to the Control Panel, and from there go to the Portal > Communities page.  At the top you'll see an Add button, click it and add a new community named Alpha.

Once the Alpha community has been created the second step is to go to Manage Pages by clicking the Actions button and selecting Manage Pages.  You'll need to add at least one page to your new community, so add a new page named Home.  Once this is done, click on the Settings tab.  In settings, click on the Virtual Host tab and you will see an empty field labeled Public Virtual Host.  Add the domain www.alpha.com here and click Save.

Now if you visit http://www.alpha.com:8080/ and you should see the Alpha community, without any pesky /web/guest in the URL.

That's it!  Now you're using virtual hosting on your machine, and your website just lost ten pounds.

And for the truly visual, I've made a video highlighting the entire process in just one minute and in one continuous shot.


P.S: If you didn't notice, you can map any number of domains and even sub-domains (xyz.a.com) to your site, but as far as I can tell Liferay does not yet support multiple domain mapping on the same community or wildcards, which means I'll be bugging our developers for the next few weeks.

Threaded Replies Author Date
Great post Jon! Informative and fun! Thiago Leão Moreira July 30, 2009 9:10 PM
Yes great feature, we're using it for quite... Rob Sonke July 30, 2009 10:25 PM
Nice - and just in time (for me). This also... Olaf Kock July 31, 2009 1:25 AM
speaking about glitches: Be aware of i18n... Olaf Kock August 4, 2009 3:53 AM
We've had some problems with the URL-generation... Henry Hagnäs August 4, 2009 4:09 AM
Hi I have a question If we use a site with... Mitesh S Panchal June 23, 2011 12:03 PM
I also have written an article about virtual... Rodrigo Martins December 13, 2012 7:15 AM
Great Post Jon! Thanks for sharing this with... Jeffrey Paul Handa July 31, 2009 6:17 AM
Great Post Jonathan , Liferay Portal is simply... Suresh Nimmakayala August 2, 2009 2:48 PM

Great post Jon! Informative and fun!
Posted on 7/30/09 9:10 PM.
Yes great feature, we're using it for quite some time now. The multiple virtual hosts would be a great improvement. Maybe you can take multilanguage into account too? If you reach a site with a certain hostname, show the website in a specific language.
Posted on 7/30/09 10:25 PM.
Nice - and just in time (for me). This also works when liferay doesn't live in "/" but - e.g. - in "/cms". We use Apache and mod_jk and have some static content that is served by apache, so in order to ease mapping of directories we have relocated liferay from "/" to "/cms" (or, in other instances, to different directories)

With mod_jk in place virtual hosting seems to work well - apart from the occasional glitches (see http://issues.liferay.com/browse/LPS-3210 and http://issues.liferay.com/browse/LPS-3482 - note: both issues are unrelated to virtual hosts but related to deploying to a location other than "/").

Just add your liferay-deployment-directory to the path and you have a nice URL as well.
Posted on 7/31/09 1:25 AM.
Great Post Jon! Thanks for sharing this with the community.
Posted on 7/31/09 6:17 AM.
Great Post Jonathan ,
Liferay Portal is simply Awesome and love the Direction and the way it is going.

as liferay give so much for the community( i mean virtualhost, managepages) feature why not authentication setup for community. because different community comes from different urls looks like different site share the information in the backend. for Liferay portal administrator community based authentication feature is going to be so much helpful and ofcourse for Liferay also.
Posted on 8/2/09 2:48 PM.
speaking about glitches: Be aware of i18n issues with the url (or your friendly url) - they don't work well together:


Luckily the quickfix is to just choose another friendly url. Just have to remember this fix for StringIndexOutOfBoundsException, but if you don't know this, it is quite a show stopper.
Posted on 8/4/09 3:53 AM in reply to Olaf Kock.
We've had some problems with the URL-generation when using VirtualHost aswell, seems like the VirtualHost-parts haven't been as well tested as the rest of the Portal. In addition to the above mentioned 3210 i've had the following problem http://issues.liferay.com/browse/LPS-3626 with wiki-links missing whitespaces when using Virtual Hosts.
Posted on 8/4/09 4:09 AM in reply to Olaf Kock.
Hi I have a question

If we use a site with context e.g. http://www.mydomain.com/myroot/web/guest/home
Now if need to remove /web/guest here
what should be done?
Posted on 6/23/11 12:03 PM in reply to Henry Hagnäs.
I also have written an article about virtual host:
Furthermore, there are some tips on breadcrumb problem.
Posted on 12/13/12 7:15 AM in reply to Mitesh S Panchal.