Carl
Carl

Reputation: 1276

URL Redirect in ColdFusion 8 running on IIS

I have a client that want's an alias directory name to automatically re-direct to a specific template...

Alias

http://www.host.com/thisplace

Real Path

http://www.host.com/somewhere/file.cfm?var=123&anothervar=567

Any suggestions on the best way to do this?

Upvotes: 0

Views: 337

Answers (2)

Rick Groenewegen
Rick Groenewegen

Reputation: 111

I would prefer a solution with URL rewrites like Mart A Kruger describes, but if you're looking for quick solution: Add the following to your onRequestStart method in your Application.cfc:

<cffunction name="onRequestStart" returnType="boolean" output="true">

    <cfset sRedirects = structNew()/>
    <cfset sRedirects["/thisplace"] = "/somewhere/file.cfm?var=123&anothervar=567"/>
    <cfset sRedirects["/otherplace"] = "/somewhere_else/file.cfm?var=456&anothervar=abc"/>

    <cfif structKeyExists(sRedirects,CGI.PATH_INFO)>
        <cflocation url="#sRedirects[CGI.PATH_INFO]#" statuscode="301"/>
    </cfif>

</cffunction>

Again, its a quick-fix and URL rewrites are definitely the way to go in my opinion.

Upvotes: 0

Mark A Kruger
Mark A Kruger

Reputation: 7193

There are several ways to do this. One way is to set up the 404 handler as a cfm page, then add code to the cfm page to tease out the file name or other variables you are looking for. This works and even works well - but there are some costs associated with it. Here's a link that shows that approach in IIS:

http://mkruger.cfwebtools.com/index.cfm?mode=entry&entry=8F4658E4-0763-5FB7-67D23B839AB74005

We use this approach successfully on a site that serves up HTML 5 "brochures" for marketing. Our admin allows for various keywords to be added and then example.com/keyword triggers the 404 handler -which looks up the keyword and serves the right brochure. So you could do example.com/eatAtJoes - and Joe's cafe brochure would be served. It's flexible and doesn't require lots of care and planning. The keywords simply must be unique and can't reflect any actual folders under the root - that's it. But it does mean triggering a 404 error for every potential keyword - which is not always optimal.

Another way would be to use URLRewrite - but that might involve adding something to your url patter like /go/somewhere - and then using /go/ to identify the pattern and /somewhere to determine the URL var. This approach is the least costly (in terms of web server resources) but might involve more structural changes - i.e. you have to alter your URLs.

Upvotes: 1

Related Questions