Friendly URLs for Railo in Cloudbees

May 9 2011

Friendly URLs for Railo in Cloudbees

Posted by AJ Mercer at 4:18 PM cfwheels | cloudbees | Railo

Railo was so easy to deploy to the 'cloud' using Cloudbees RUN@Cloud. Mark Drew has written up how to deploy Railo to Cloudbees.

I have been messing around with this just to see what 'cloud' computing is all about and just for fun uploaded CFWheels to http://cloudbees.webonix.org (http://cfwheels.webonix.cloudbees.net/ cloudbees lets you add your own domains). But for a wheels site to look sexy it needs friendly URLs.

This is achievable using Tuckey URL Rewrite Filter which I discovered on Aaron Greenlee's website. Extract to the WEB-INF in the root of your website. Edit the WEB-INF/web.xml and add this before the servet tag

    <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>

While you are in this file, also add the following with the rest of the CFML mappings. Tomcat does not allow for multiple wildcards

<servlet-mapping>
<servlet-name>CFMLServlet</servlet-name>
<url-pattern>/rewrite.cfm/*</url-pattern>
</servlet-mapping>

Next Create urlrewrite.xml in WEB-INF. This is what I have based on comments in CFWheels doco

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
 <!--
  
    Configuration file for UrlRewriteFilter
    http://tuckey.org/urlrewrite/
  
-->
<urlrewrite>
    <rule>
        <note>
            Allow TomCat to have SES links.
        </note>
        <!--
            If our URI ends with one of these extentions we will not perform
            a URL rewrite.
        -->
        <condition type="request-uri" operator="notequal">\.(bmp|gif|jpe?g|png|css|js|txt|pdf|doc|xls|xml|cfc|ico|php|asp)$</condition>
        <!--
            If any URI begins with one of these directories, we will not
            perform a rewrite. Helpful for running your unit tests or
            "blocking" rewrite for some directories.
        --> 
<condition type="request-uri" operator="notequal">^/$</condition>
       <condition type="request-uri" operator="notequal">^$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/railo-context(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/flex2gateway(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/jrunscripts(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/cfide(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/cfformgateway(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/files(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/images(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/javascripts(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/miscellaneous(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/stylesheets(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/robots.txt(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/sitemap.xml(.*)$</condition>
       <condition type="request-uri" operator="notequal">^(.*)/rewrite.cfm(.*)$</condition>
        <from>^/(.*)$</from>
        <to>/rewrite.cfm/$1</to>
    </rule>
</urlrewrite>

Now you can start cloudbees for your site and you should be ready for creating freindly URLS. Which for CFWheels means editing config/settings.cfm and adding 

<cfset set(URLRewriting="On")>

And builing links using linkTo(). E.g.

<h1>Page One</h1>
<cfoutput>
#linkTo(text="Page Two", controller="ses", action="pageTwo")#
</cfoutput>

Now you are ready to deploy to the cloud and end up with something like this http://cloudbees.webonix.org/ses/page-one

Comments

Ryan

Ryan wrote on 07/15/11 6:20 AM

Hi AJ,

I am following along here and hit an issue. I do not have a web.xml in my webroot/WEB-INF. I assume because I used the installer. Should I create a web.xml in my webroot/WEB-INF dir and add the above there or should I put it in the /opt/railo/tomcat/conf/web.xml file? thx
AJ Mercer

AJ Mercer wrote on 07/15/11 6:33 AM

create a new web.xml in webroot/WEB-INF

Make sure you check out Aaron Greenlee's page (link in blog) as well - this has more deails.
Jusuf Darmawan

Jusuf Darmawan wrote on 08/04/11 12:53 PM

I followed all the detail above and for some reason my URL has index.cfm. For example: http://www.mydomain.com/index.cfm/go/post. How to erase the /index.cfm/ in between. Thanks.
AJ Mercer

AJ Mercer wrote on 08/04/11 12:56 PM

@Jusuf if you take the index.cfm/ out of the URL does it still work?

If so, what ever is creating the URLs need to be modified.
Jusuf Darmawan

Jusuf Darmawan wrote on 08/04/11 11:39 PM

Hi AJ, Yes it worked fine if I took out the index.cfm. Currently, I am not sure what causing this. I copied the cfwheels 1.1.5 and modified the web.xml and urlrewrite.xml as following above and automatically the URL added /index.cfm/ and go to the subpage it's getting uglier. I had a feeling the problem is coming from urlrewrite.xml but don't know how to fix that.

Here is my urlrewrite.xml: THANKS

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">;
<!--

Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/

-->
<urlrewrite>
<rule>
<note>
Allow TomCat to have SES links.
</note>
<!--
If our URI ends with one of these extentions we will not perform
a URL rewrite.
-->
<!--
If any URI begins with one of these directories, we will not
perform a rewrite. Helpful for running your unit tests or
"blocking" rewrite for some directories.
-->
      <condition type="request-uri" operator="notequal">^/$</condition>
<condition type="request-uri" operator="notequal">^$</condition>
<condition type="request-uri" operator="notequal">^(.*)/railo-context(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/flex2gateway(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/jrunscripts(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/cfide(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/cfformgateway(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/files(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/images(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/javascripts(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/miscellaneous(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/stylesheets(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/robots.txt(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/sitemap.xml(.*)$</condition>
<condition type="request-uri" operator="notequal">^(.*)/rewrite.cfm(.*)$</condition>
<from>^/(.*)$</from>
<to>/rewrite.cfm/$1</to>
</rule>
</urlrewrite>
AJ Mercer

AJ Mercer wrote on 08/05/11 7:25 AM

Hi Jusuf ,

you are one step short, you need to add the following to your settings (and restart wheels)

set(URLRewriting="On")

It is in the comments of
http://cfwheels.org/docs/1-1/chapter/url-rewriting
Jusuf Darmawan

Jusuf Darmawan wrote on 08/14/11 12:54 AM

Hi AJ,

I just figured out last night that I missed to set up the /index.cfm/* inside the <servlet-mapping /> so now everything it's working correctly. Thanks for your help.
Mohamad

Mohamad wrote on 09/16/11 11:53 PM

AJ thanks for this guide.

Dumb question alert: when you say unzip it's contents into WEB-INF, do you mean just the jar file, or also the lib folder?

The zip file has this /root/lib/file.jar

Do we want to end up with:

/WEB-INF/file.jar

or:

/WEB-INF/lib/file.jar
AJ Mercer

AJ Mercer wrote on 09/18/11 7:16 PM

keep the directory structure
Molnfront

Molnfront wrote on 12/20/11 7:36 AM

Great post!

The servlet-mapping part was the missing piece to have my Railo/Cloudbees/Coldbox set-up to work!
Joe Genshlea

Joe Genshlea wrote on 06/25/12 2:31 AM

the servlet mapping declaration appears to blow things up. Without it, Tomcat & railo apear to run, rewrite does not work.

when I add it I got

java.lang.IllegalArgumentException: Servlet mapping specifies an unkn
own servlet name CFMLServlet

So then I went to web.xml in the tomcat/conf dir and noticed that the servlet mapping use "GlobalCFMLServlet" so I tried that. No Good, although I don't see any error dumps in the trace.

The other contexts (ones without urlRewrite) are loading okay.

Any ideas?
Joe Genshlea

Joe Genshlea wrote on 06/25/12 3:06 AM

Ignore part of that....I noticed the servlet tag must also be in the web.xml file. I added the servlet declarion, and the context loaded fine. URLRewrite still not working though :(
AJ Mercer

AJ Mercer wrote on 06/25/12 6:36 AM

Hi Joe,

I replied to your post on CFWheels - if you have Apache you do not need to do all this - it has it's own rewrite module (which is a lot easier to set up)