Managing URL Rewriting

Defining pretty URL for accessing and configuring a Mashup UI page is a very common use case for business-focused Apps and eCommerce.

For example, we want to rewrite the URL: http://www.mysite.com/mashup-ui/page/search?type=clothes&products=shirts to map it to a specific product refinement on the /products page and get a cleaner URL like: http://www.mysite.com/products/clothes/shirt

To handle URL rewriting, we use Url Rewrite Filter version 3.2. For more information, see the documentation at: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html#filterparams

Important: The rewriting rules you define take precedence on the default Mashup UI behavior and may lead to errors. For example, some widgets allow exporting data through the service http://www.mysite.com/export. If your rule rewrites all pages to http://www.mysite.com/<PAGENAME>, requests for /export will redirect to a page called export instead of the export service. To prevent this, you should set exception rules to redirect requests to the correct service.

This task shows you how to:

Enable URL rewriting

  1. Go to <DATADIR>/webapps/360-mashup-ui/WEB-INF/
  2. Edit the WEB-INF/web.xml file.
  3. Uncomment the UrlRewriteFilter filter node.

    <filter>
      <filter-name>UrlRewriteFilter</filter-name>
      <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
      <init-param>
        <param-name>logLevel</param-name>
        <param-value>log4j</param-value>
      </init-param>
      <init-param>
        <param-name>confReloadCheckInterval</param-name>
        <param-value>5</param-value>
      </init-param>
      <init-param>
        <param-name>statusEnabled</param-name>
        <param-value>false</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>UrlRewriteFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

  4. Save and close the file.

Configure URL rewriting

  1. In the <DATADIR>/webapps/360-mashup-ui/WEB-INF/ directory, edit the WEB-INF/urlrewrite.xml file.
  2. Uncomment the content of the urlrewrite node, and add/edit the rewriting rules as needed. For our example of /products/clothes/shirt we would have the following configuration.

    <urlrewrite>
    
      <!-- Services have the highest priority -->
      <rule enabled="true">
        <name>Services</name>
        <from>^/(resources|fetch|alerting|export|utils|login|errors|lang|storage|logout
    |staging-builder|testProduction)(.*)?</from>
        <to last="true">/$1$2</to>
      </rule>
    
      <!-- redirects /page/index to / -->
      <rule enabled="true">
        <name>RedirectIndex</name>
        <from>^/page/index$</from>
        <to last="true" type="redirect">%{context-path}/?%{query-string}</to>
      </rule>
    
      <!-- redirects /page/pageName to /pageName -->
      <rule enabled="true">
        <name>RedirectPages</name>
        <from>^/page/(\w+)$</from>
        <to last="true" type="redirect">%{context-path}/$1?%{query-string}</to>
      </rule>
    
      <!-- / -->
      <rule enabled="true">
        <name>Index</name>
        <from>^/$</from>
        <to last="true">/page/index</to>
      </rule>
    
      <!-- /pageName -->
      <rule enabled="true">
        <name>Pages</name>
        <from>^/(\w+)$</from>
        <to last="true">/page/$1</to>
      </rule>
    
      <!-- /products/clothes/shirt -->
      <rule enabled="true">
        <name>Search</name>
        <from>^/(\w+)/(\w+)/(\w+)</from>
        <to>/page/search?type=$1&amp;$2=$3</to>
      </rule>
    </urlrewrite>

    Note that:

    • First, all the services that can be called by the Mashup UI (export, alerting, storage, etc.) are processed. We make sure they will not be impacted by URL rewriting to avoid errors. We can then add filter rules for the URLs of the application pages.
    • /page/ is hidden from the URL
    • The / redirects to the index page
  3. Save and close the file.