[thelist] Search-engine safe URLs in ColdFusion (was: Latest IE release ...)

Seth Bienek evolt.list at sethbienek.com
Thu Oct 3 15:48:01 CDT 2002


Hi Bob,

The issue I'm currently having isn't in implementing SSURL's, though
I've done it before.  I thought there was an article on Evolt already on
how to do this, but I couldn't locate it, sooo....

Here's a quick rundown:

<tip type="ColdFusion">
So, you want to create search-engine-safe URLs in ColdFusion.

Here's what you'll need:
	o CF app server
	o Knowledge of how your webserver implements
	  custom errors (namely 404)
	o Table (or a structure, or whatever you prefer)
	  of short-cut URL's and their actual equivalents.

First the table.  Needs two columns at least, we'll call the table
'url_forward':
                   url_forward
 +----------+---------------------------------------+
 | shortcut |                 actual                |
 +----------+---------------------------------------+
 | /bob     | /users/info/userdata.cfm?username=bob |
 | /abc-corp| /cobranding/index.cfm?brand=abc_corp  |
 +----------+---------------------------------------+

I'm sure you can see where this is going.

Now is the easy part.  Building the actual forwarder (we'll name it
ssurl_checker.cfm, just for giggles).  Basically, what we are going to
do is look at the URL, check our rable to see if it should be mapped to
an actual url, and then forward the user on to the correct URL, or the
'real' 404 page, whichever is appropriate.

The forwarder will look something like this:
<!--- ssurl_checker.cfm --->
<!--- Trim protocol and server data from URL --->
<cfset urlstring = "/" & listrest(listrest(listlast(cgi.query_string,
";"), "/"), "/")>

<!--- Query DB for matches. --->
<cfquery name="geturl" datasource="#request.dsn#">
	SELECT actual FROM url_forward where shortcut = '#urlstring#'
</cfquery>

<cfif geturl.recordcount>
	<!--- Send user to correct URL --->
	<cflocation url="#geturl.actual#" addtoken="no">
<cfelse>
	<!--- This will point to your real 404 page, which you can
	create from scratch or copy into your webroot from IIS'
	original location. --->
	<cfinclude template="404.htm">
</cfif>

(Note that this is off the cuff, untested code.  Don't expect to plug it
in and it work, you'll likely want to tweak it to your standards
anyway.)

Next, any decent webserver program has an option to allow you to set up
custom error handlers.  In IIS, open the Internet Services Manager,
right-click on the website you want to change this setting for, then
select 'Properties'.  On the Web Site Properties page that comes up,
select the "Custom Errors" tab, then scroll down to HTTP Error '404'
(NOT 401;4, a common mistake, but 404.  It's down there, I promise).
Select 404, click 'Edit Properties', set 'Message Type' to URL, and then
enter a relative path to the error-handling template (which is also our
url-forwarding app, "\ssurl_cheker.cfm" in our case).

That's it!  Easier than you thought it would be, eh?

An interesting twist on this utility is to use different parts of a url
to build a dynamic request, i.e.:
http://mydomain.com/book/14/chapter/1
To represent:
http://mydomain.com/books/showchapter.cfm?book=14&chapter=1

This can be done using the same method as above, the difference being
that you use only the script_name in the 'actual' column of your table,
and have your "ssurl_checker" parse the url for dynamic values:

<cfset querystring = "?">
<cfloop list="#urlstring#" delimiters="/" index="i">
	<cfif listfind(urlstring, I, "/") mod 2>
		<cfif listfind(urlstring, I, "/") gt 1>
			<cfset querystring = querystring & "&">
		</cfif>
		<cfset querystring = querystring & i & "=">
	<cfelse>
		<cfset querystring = querystring & i>
	</cfif>
</cfloop>

<!--- Then pass the "homemade" querystring with the "actual" url --->
<cflocation url="#geturl.actual##querystring#" addtoken="no">

One last disclaimer: This code is purely demonstrative, and should be
streamlined and validated before actual production use.  It's just to
give you an idea, so feel free to keep any bugs or inefficiencies to
yourself. :)
</tip>

Wow, that was longer than I planned.

Hope it helps!

Seth


> -----Original Message-----
> From: Bob Haroche
>
> As an aside (favor), could you share the snippet of
> <CFLOCATION> code you're using for that
> re-mapping/re-direction. I'm trying to implement the same
> thing in my custom 404 page on an IIS server and I can't
> figure out how to detect the non-existent page as requested
> by the user agent.





More information about the thelist mailing list