Flexible Friendly URLs for Farcry with Mod rewrite

I've known about and used Spike's friendly URL servlet which does a great job of handling friendly URLs for quite a while, but recently I was shown an alternative way by Gav Cooney of Websonic.

This alternative method uses apache's mod_rewrite functionality to dynamically rewrite the url before passing it to ColdFusion. This example uses the exact same format as the FU servlet so it should be possible to drop it into place.

So without further ado - here is what you do.

Put this in the virtual host section of your apache config:

RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
RewriteRule ^/go([^\?]*)(?:\?(.*))? /go.cfm\?path=/go$1&$2 [PT,L,QSA]

Advantages

  • Simple to change the prefix: i.e. /go/ or use multiple different ones
  • Can add additional parameters
  • Moves the load from java to apache, which is easier to scale

Disadvantages

  • Not available on IIS - which isn't really a disadvantage :-)

Here's and example of some of the flexibility: Multiple working urls which pass a parameter - /ie/ add language=ie and /en/ adds language=en

RewriteRule ^/en([^\?]*)(?:\?(.*))? /go.cfm\?path=/go$1&$2&language=en [PT,L,QSA]
RewriteRule ^/ie([^\?]*)(?:\?(.*))? /go.cfm\?path=/go$1&$2&language=ie [PT,L,QSA]

Hope it helps, and love to hear your thoughts or comments.

Updated - changed regex to always use the start of the url - see related article.

Update 2 - another fix to the regex - see related article

Comments
TJ Downes's Gravatar It's actually a significant disadvantage not to be IIS compliant if you want your applications to be truly portable or open source.

As much as some do not like it, IIS is a very stable and commonly used web server. Unlike PHP development, it is fairly easy to make CF apps cross-platform compatible and I believe that is a significant advantage CF developers should take full advantage of.

There are alternatives to SES URLs in CF for IIS, including doing it natively in CF, using URL Rewrite (http://www.iismods.com/url-rewrite/index.htm, I have not used this so I cannot speak for it), and others.
# Posted By TJ Downes | 1/7/07 7:24 PM
Mark Lynch's Gravatar Hi TJ,
I have to say I agree with you - I was just being faecetious.

I'm glad to see that there is a open source solution for URL rewriting on IIS - last time I looked there wasn't (or I could find one 5 years ago).

Thanks for reading,
Mark
# Posted By Mark Lynch | 1/7/07 7:36 PM
Jeff Coughlin's Gravatar Nice post Mark.

Actually there are plugins for IIS that do mod rewrites (and you can use similar RegEx syntax) and they work with FarCry (see wiki link below).

The mod_rewrite that comes with Apache 2.x is a plugin that comes pre-installed Apache (and has for some time).

But wait, is it possible to use a rewrite tool in IIS without having to install a third-party plugin? Microsoft has said for quite a while now that the next version of Windows server will have an officially supported pre-built rewrite plugin (but I haven't looked into it myself).

For more tutorials on using mod rewrite (for both IIS and Apache) for use with FarCry checkout the FarCry Wiki:
http://docs.farcrycms.org:8080/confluence/display/...
# Posted By Jeff Coughlin | 1/8/07 9:01 AM
TJ Downes's Gravatar Hey Mark, no biggie :) I am used to people poking fun at MS. I've used IIS for some pretty high-trafficked sites and have found it to be secure and reliable if a person configures it properly. Additionally it requires Windows be properly configured and secured as well, which is were a lot of the blame lies for issues people have with IIS.

Jeff, I am pretty certain you are doing the same thing natively in CF when you use this method, but correct me if I am wrong:http://www.adobe.com/cfusion/knowledgebase/index.c...
# Posted By TJ Downes | 1/8/07 1:34 PM
Mark Lynch's Gravatar Hi TJ,

My single biggest problem with IIS is the fact that you can't run a development environment on your workstation very easily if you are using IIS. As far as I recall you can only have a single website on and XP machine using IIS. How many web developers do you know that only develop one website? http://channel9.msdn.com/ShowPost.aspx?PostID=2343...

That's why I switched to using apache on windows and then to apache on linux - as I could set up my development environments to match my production ones very simply. And I only have to edit a text file instead of the magical mystery metabase.

Cheers,
Mark
# Posted By Mark Lynch | 1/8/07 1:56 PM
TJ Downes's Gravatar Hey Mark, i feel your pain there. I stopped making the effort to develop using IIS on my workstation years ago and keep a development server for such purposes. Hopefully MS one day gets how inconvenient it is for developers and choose to allow us to run more.
# Posted By TJ Downes | 1/8/07 2:30 PM
Robb's Gravatar Actually it is possible to have multiple websites on your local IIS XP Pro development machine with one caveat though, and that is; they can't all be running at the same time you must stop and start the websites manually, but quite frankly that's no big deal for me as I rarely work with more than one site at a time anyhow.

Save the following lines to a text file:
This assumes default setup with IIS and example files:

C:\Inetpub\AdminScripts\adsutil.vbs create_vserv W3SVC/2

C:\Inetpub\AdminScripts\adsutil.vbs copy W3SVC/1 W3SVC/2

Open IIS Management console >>
click on the Web Sites folder Icon >>
right click on your web Default folder (or whatever its named) >>
select Properties >>
enable Logging (if not already logging, this is just temporary) >>
select Properties next to the Active Log Format >> make a note of the log directory name
we just want the directory name which most likely will be W3SVC1 but... just to be safe make a note of the directory name >>
Cancel (uncheck logging enabled if it was unchecked before) >>
close the Properties window >>
select Windows Start >>
select Run >>
copy line 1 of your text file and paste in Run text box >>
hit OK.. you now created a new web site >>
copy line 2 of your text file into the Run text box >>
hit OK .. you now have copied your configuration from the existing site to the new web site. It is now an exact duplicate of the original default web site.
ALMOST DONE!!
Tab back to your IIS Management Console and you should now see two website Folders with the exact names except one will be running and one will be stopped Right click on the stopped web Folder Icon and select Rename site >>
Enter a new name >>
Go to Properties on the newly named site and set a new home directory and save (I keep all of my different project folders on my dev machine under wwwroot just to make it easy but it shouldn't matter). >>
Right Click on the the current running site and select STOP >>
Right Click on the new site and select Start >>
in your browser go to http://127.0.0.1 and your new site should come up. >>
You can edit your Host file >>
Windows Start >> Run >>
paste the following: %WINDIR%\system32\drivers\etc\hosts >>
OK >>
Select Notepad/Wordpad and add an entry to the file like: 127.0.0.1 newSite save >>
In your browser go to http://newSite and the site should work.

NOTE: keep track with your text file on what site you copied and the last site made. Next site you make should be:

C:\Inetpub\AdminScripts\adsutil.vbs create_vserv W3SVC/3
C:\Inetpub\AdminScripts\adsutil.vbs copy W3SVC/1 W3SVC/3

Save the text file.

Easy thing is to create a template website and use this one to build all your new ones.

Good Luck

Robb
# Posted By Robb | 1/23/07 7:42 AM
Mark Lynch's Gravatar Hi Robb,

Thanks for that - it's good to know it can be done. I'll stick with Apache though - it makes it so much easier to have them as many sites running as you want. It also means you can cut and paste to create new websites.

Cheers,
Mark
# Posted By Mark Lynch | 1/23/07 9:46 AM
Robb's Gravatar If you don't like the manual way there is a free utility that will set up the sites for you and let you manage them. The tool is called XP Pro IIS Admin and can be downloaded here: http://jetstat.com/iisadmin/ I have absolutely no connection with this software except I use it and it has been great on my dev machine. I just wish it would update my host file for me automatically but definitely worth checking out very very cool freeware.

Robb
# Posted By Robb | 1/23/07 1:45 PM
Mark Lynch's Gravatar Hi Robb,

Why is there any need to change your hosts file - just add every site in with the same ip address and you'll be right - or are you using the domain names for something else?

Cheers,
Mark
# Posted By Mark Lynch | 1/23/07 7:36 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.