Friendly URLs in Liferay

By default, Liferay portlet URLs contain many parameters like portlet mode, portlet life cycle, etc., which makes URLs very lengthy and complex. Sometimes we want to share a friendly URL, which is human readable and meaningful. Also, we may not want to expose all those parameters in the browser.

Liferay provides a friendly URL mapping capability, where we can define our custom URL pattern along with important parameters and hiding unwanted parameters from the URL path.

Here is an example of implementing the friendly URLs feature in Liferay.

Implementing the Liferay Friendly URLs

Create a new Liferay portlet and add the following tags inside liferay-portlet.xml after the <icon> tag.

The configuration here defines the logic for how to map friendly URLs to regular Liferay URLs.

We can use Liferay’s DefaultFriendlyURLMapper class to add a friendly URL prefix.

The <friendly-url-routes> tag specified in the XML file defines the friendly URL routes.


Create a xml file(my-url-routes.xml) inside src directory (under com/asb/urlContent package)and add the following content.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.1.0//EN" 

 <generated-parameter name="mvcPath">/html/friendlyurl/{pagename}.jsp</generated-parameter>

Here, the <pattern> tag defines the friendly URL pattern. Each Liferay friendly URL will contain its own <route> tag with associated parameters.

Friendly URLs start with / followed by the defined prefix and URL pattern.

Here /{myParam:\d+}/{pagename}/ specifies that friendly URL contains a parameter named myParam, which is a number and followed by a string which is a jsp page.

Create two jsp pages to test the friendly URLs. For example, view.jsp and secondPage.jsp

Add the following code to the secondPage.jsp file.

Here we are sending a parameter called “myParam” through a friendly URL and printing it in seconPage.jsp.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<%String param = request.getParameter("myParam");%>
<h1><%=param %></h1>

Add the below code in the view.jsp file.

<%@ taglib uri="" prefix="portlet" %>
<portlet:defineObjects />

This is <h1>View.jsp</h1>
<a href="web/guest/home/-/my-url/secondPage/" >Go to second page</a>

These jsp files can be accessed now using the following friendly URLs:

http://localhost:8080/web/guest/home/-/my-url/5/secondPage and, http://localhost:8080/web/guest/home/-/my-url/view

Testing the output

We can access the portlet using the friendly URL pattern as shown below.


Click on the link and once the URL is redirected, check the URL pattern.



In this article, we learned about Liferay friendly URLs.