0

I would like to know if there's an easier way other than Mod Rewrite (using the fusebox framework or directly in Coldfusion) to convert a url as follows:

from:

http://www.somedomain.com/salmahayek
or 
http://localhost/someApp/salmahayek

to:

http://www.somedomain.com/index.cfm?action=profile.view&name=salmahayek
or
http://localhost/someApp/index.cfm?action=profile.view&name=salmahayek

My app is an existing Fusebox 5.5 application.

I just need to add that the url above is not static, i.e. "salmahayek" could be any name.

Any help would be greatly appreciated Thanks

Paolo Broccardo
  • 1,942
  • 6
  • 34
  • 51

4 Answers4

1

You could potentially use the "classic" way of doing it (not sure if Fusebox will interfere), using a 404 handler, something like this should do the trick:

  1. Set up a 404 hander on your server, e.g. in .htaccess:

    ErrorDocument 404 /404handler.cfm

  2. set up 404handler.cfm to wrap around the framework, e.g.:

    <cfset variables.checksok = false>
    <!--- do some checks - example --->
    <cfif cgi.REDIRECT_URL EQ 'salmahayek'>
        <cfset variables.checksok = true>
    </cfif>
    <cfif variables.checksok EQ true>
        <cfheader statuscode="200" statustext="OK">
        <cfset url.action = "profile.view">
        <cfset url.name = cgi.REDIRECT_URL>
        <cfinclude template="index.cfm">
    </cfif>

(not tested but should work)

Ben
  • 3,922
  • 1
  • 22
  • 21
  • IIS will support the same. It is a hack but it won't fail. – Aaron Greenlee Dec 01 '10 at 19:00
  • Hi - Sorry - I meant to add that the url above is not static, i.e. "salmahayek" could be any name, so that hard coded entry probably wouldn't work. – Paolo Broccardo Dec 01 '10 at 19:18
  • Cheeky - as per the coldfusion comment, it was just an example, you probably want to replace it with a better check, e.g. making sure it exists in the database or so, rather than blindly sending anything that 404s through the profile.view action. – Ben Dec 01 '10 at 19:24
1

I've doing some like this in one my apps currently, albeit in PHP:

http://localhost/index.cfm/profile.view/salmahayek/

<cfset urlArgs=listToArray(CGI.PATH_INFO, "/") />
<cfset action=urlArgs[1] />
<cfset name=urlArgs[2] />

This works perfectly, but you have to put up with the "index.cfm" if you don't want to rewrite.

Jere
  • 3,377
  • 1
  • 21
  • 28
0

I'm not sure about anyone else, but I don't understand why Mod Rewrite would be difficult, unless you are on IIS. A rewrite rule would simply have to be something like:

^(login|register)/([^/\.]+) index.cfm?action=profile.$1&step=$2 [L]
^([^/\.]+)/?$ index.cfm?action=profile.view&name=$1

I put in some extra examples to check if the user is actually trying to get to the registration or login page and what step they are on there.

Dave Long
  • 9,569
  • 14
  • 59
  • 89
0

i've actually done this in past using the Application.cfc's onMissingTemplate() method. you can either do some regexs against the arguments.targetpage that gets passed in or do a lookup in a database. either way you would do a cflocation to the correct page afterward. just remember to pass over any url parameters also.

one thing that i've never tried out and often wondered though is if this could be handled in the onRequestStart() method instead? the biggest problem i have with using onMissingTemplate() is that you're doing a cflocation which is a completely new request and you can't pass through form variables. yes i know you could probably use GetPageContext().Forward( strUrl ) instead, but you're still going threw the entire request lifecycle in for the original request. by doing this in onRequestStart() you would avoid this.

anyone want to test this out?

rip747
  • 9,375
  • 8
  • 36
  • 47