Shaun Mccran

My digital playground


Using Isapi rewrite to serve up non existing templates

I was discussing some ideas for an application framework this morning with the team, and one of the issues we hit upon was having a common directory for templates, but serving them up as if they were from a different directory.

The idea is to have one instance of a reusable skinnable template, that appears to live on several sites.

IE all the content lives in "webroot/content/templateName.cfm", but is actually served up by many sites, IE "", "" ... etc

In this way they can be re skinned or adapted as needed, and they aren't database driven. The main stumbling block for the discussion was the need to actually create blank versions of each of the named templates, in each of the sites, as ColdFusion server would error on the request.

I spent twenty minutes trying to work it so that my Application.cfc's onRequest or onRequestStart method would intercept the request before it was actually made, but that just wasn't working. My other idea was to use the onMissingTemplate method, but the server is only running ColdFusion 7, so that was a no go (I figured I could catch the missing template request and just re path it, although I'd have to assess if that was really inefficient due to almost every page request logging as failed).

My eventual solution was Isapi rewrite. I am re writing all the requests to the same template, and just passing in the template variable. In that way I can request pages that don't actually exist, but they appear in the url.

Create an index.cfm template like this:

view plain print about
1<h1>I am the index page</h1>
3    <li><a href="page1">Page 1</a></li>
4    <li><a href="page2">Page 2</a></li>
5    <li><a href="page3">Page 3</a></li>
6    <li><a href="page4">Page 4</a></li>
9<cfdump var="#url#">
10<!--- write a handler to go get the url var passed in --->

For this example I am using the free version of Helicon's Isapi rewrite, you can get it here: Link to Helicons Isapi re write

In the example below I have altered the first page link to look like it is actually a .cfm template request, just in case you want the url string to have a .fileextension look to it.

view plain print about
1# Helicon ISAPI_Rewrite configuration file
2# Version
4RewriteEngine on
5RewriteBase /mywebroot
7#no physical page testing
8RewriteRule page1.cfm(/)? isapitest/index.cfm?p=page1
9RewriteRule page2(/)? /index.cfm?p=page2
10RewriteRule page3(/)? /index.cfm?p=page3
11RewriteRule page4(/)? /index.cfm?p=page4

So when you fire it up and test it you just see /page1, /page2 etc, and the pages don't actually exist.

I'm not experienced enough with Isapi rewrite to know if there is a downside to this, but bookmarking in a browser still works correctly, so I can't see an issues at present.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Freelance Web Developer's Gravatar I use ISAPI_rewrite for vanity/sef urls on just about all the sites I make. Here is my .htaccess file, which is good for 4 levels of pages:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

# Block external access to the Helper ISAPI Extension
RewriteRule ^.*\.isrwhlp$ / [NC,F,O]

# 301 Redirect all requests that don't contain a dot or trailing slash to include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !\?
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /section/topic/page/subpage/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [S=4]
RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ /includes/cfm/sefurl.cfm?sec=$1&top=$2&pag=$3&sub=$4 [QSA]
RewriteRule ^(.*)/(.*)/(.*)/$ /includes/cfm/sefurl.cfm?sec=$1&top=$2&pag=$3 [QSA]
RewriteRule ^(.*)/(.*)/$ /includes/cfm/sefurl.cfm?sec=$1&top=$2 [QSA]
RewriteRule ^(.*)/$ /includes/cfm/sefurl.cfm?sec=$1 [QSA]

I don't think there is a downside to using url re-writing. If pages actually do exist, no rewriting is done.
# Posted By Freelance Web Developer | 16/12/2009 14:11
Sadiq's Gravatar These applications and framwork are always very important for me and I think for all the important people. The people should get these articles and posts from this website
# Posted By Sadiq | 06/01/2016 08:58
Back to top