
h1. Friendly URLs

h2. Purpose
With eXo WCM you can simply use the Content List Viewer portlet (CLV) in conjunction with the Parameterized Content Viewer portlet (PCV) in order to simply display a list of content and their details.
By default, WCM content URLs are pointing directly to the file located within the JCR storage structure, so the URL path contains technical elements inside. 

For example, the URL of a WCM content can be as complex as that:

{code}
http://localhost:8080/portal/private/vitrines/products/presentation/pcv/repository/collaboration/sites%20content/live/vitrines/web%20contents/site%20artifacts/Toute%20Actualite/Numero%20appel%20urgence%20europeen
{code}

In this article, we are going to see how to remove all technical information from URLs and get clean, friendly and REST-like bookmarkable URLs.

Using the tips described in this article, you can get URLs that are as clean as the following:

{code}
http://localhost:8080/portal/article/My-Personal-Article
{code}

With a little extra effort, you can get this:

{code}
http://localhost:8080/portal/article/2009/11/27/My-Personal-Article
{code}


h2. URL Rewrite Filter

Download and unzip the [http://tuckey.org/urlrewrite/#download] zip file and put the jar file somewhere in the server classpath.

Add the following lines to the exoplatform portal.war web.xml file.

{code}
  <filter>
           <filter-name>UrlRewriteFilter</filter-name>
           <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        </filter>

...
 <filter-mapping>
           <filter-name>UrlRewriteFilter</filter-name>
           <url-pattern>/*</url-pattern>
  </filter-mapping>  

{code}

Make sure to put the filter mapping *after* the other filters otherwise it will not work as expected.

UrlRewriteFilter searches the configuration file "urlrewrite.xml" in the WEB-INF directory. Use regular or wildcard expressions for the parameters.



h3. Rewriting Rule Configuration File

Put a urlrewrite.xml file in the WEB-INF directory of the portal.war. Define the following rewriting rule in the file:

{code}
<rule>
    <note>
        The rule means that requests to /article/myarticle will be forwarded to 
        /public/classic/mypage/repository/collaboration/myfolder/myarticle
        the url will be rewritten.
    </note>
    <from>/article/(.*)</from>
    <to>/public/classic/mypage/repository/collaboration/myfolder/$1</to>
</rule>
{code}

Make sure that the target URL points to the right portal site, page, repository workspace and JCR folder paths where your contents are located.






h2. Demonstration
Now let's do a simple page to show how all this works.

h3. Page Layout

Create a new page named 'mypage' in WCM, at the root of the portal navigation and put a CLV and a PCV inside.

 !page1.png! 


h3. Configuration

Configure the CLV to point to the right JCR path (where the is content) and the target page to 'mypage' because the CLV and PCV are both on the same page.
 
 !page2.png! 

h3. CLV Template Modification

Open the Sites administration / DMS Administration drive. Then go to the "/exo:ecm/views/templates/content-list-viewer/list" folder. Edit the UIContentListPresentationDefault.gtmpl content, which is the template we choose for the CLV (see also [WCM Templates|#WCM Templates]).

Locate the following block of code and insert the line below: 

*itemLink = "/portal/article/$itemName"*

{code}
...
for (Node node : currentPageData) {
   def viewNode = Utils.getNodeView(node);
   if (viewNode == null)	continue;
   String itemLink = uicomponent.getURL(viewNode);			
   String itemName = viewNode.getName();
   itemLink = "/portal/article/$itemName"
   String itemSummary = uicomponent.getSummary(viewNode);
...
{code}

Save and restart the server (to clear the template cache).



h2. Conclusion

That's it! Now you have clean and friendly URLs for your contents.
Look to URL in the address bar and to the status bar when moving the mouse over content titles in the CLV. You can see that URLs are short and clean.

 !page3.png! 


h2. Overcome the Restrictions

Some restrictions apply to this URL rewriting trick, mainly the highly generic URL that cover all kind of use-cases is replaced by a more restricted single use-case URL. This solution entails several drawbacks:
* The page name, the workspace and the folder are hard-coded in the URL rewriting rule. This rewriting rule is applied to all CLV instances that use the modified viewer template. In fact in the viewer template "UIContentListPresentationDefault.gtmpl" you hard-code the shortcut URL "/portal/article/$itemName" for which the rewriting rule is applied.
** All articles pointed by any of these CLV instances must be in the same workspace (in this example "collaboration").
** All articles pointed by any of these CLV instances must be in the same folder (in this example "myfolder").
** All articles pointed by any of these CLV instances must be in the same portal/site (in this example "classic").
** By shortcutting the URL you can only point to the same page. Pointing to other pages from a CLV that uses this template will not work.

Finally the hint after listing the restrictions: Better define a specific viewer template for this trick in order not to destroy the generic URLs of the default CLV template. Even better create several new templates as you might wish to point to different folders or pages. In this case you need several URL rewriting rules.



