Skip to main content
Participant
January 13, 2011
Answered

URL rewriting trick

  • January 13, 2011
  • 2 replies
  • 2114 views

Hi. I have SESSION.shopID variable in my application.

My images are in a /resources/img/ folder.

Now I want to do a "shop based theming" so I make a /resources/001/img and a /resources/002/img folder.

It will be nice to make the Application.cfc adding that id on request of /resources/img (without changing any reference on existing pages).

Sample:

the customer logged with shopID = 2 requests /resources/img/happy.png .

Application.cfc analyses that pattern and put " /#SESSION.shopId# " after /resources.

How can I achieve this?

    This topic has been closed for replies.
    Correct answer BKBK

    fabio.bozzo wrote:

    What I'd like is to intercept the http request for /resources/img/prova.png and redirect it to /resources/002/img/prova.png ...

    where 002 is my session variable. How can I do that, e.g. in the Application.cfc?

    Be aware that the target page will remain same when no replacement occurs, which will make ColdFusion to keep including Application.cfc recursively forever.  Hence, design your code such that newTarget and targetPage will always be different.

    <cffunction name="onRequestStart">
        <cfargument name = "targetPage" type="String" required="true">
        <!--- Assumes session.token = 002 has been set elsewhere --->
        <cfset newTarget = replaceNoCase(arguments.targetPage, "/resources/img/prova.png", "/resources/#session.token#/img/prova.png")>
        <cflocation url="#newTarget#">
    </cffunction>

    2 replies

    BKBK
    Community Expert
    Community Expert
    January 16, 2011
    <cffunction name="onRequestStart">
        <cfargument name = "targetPage" type="String" required="true">
        <!--- Assumes session.token = 002 has been set elsewhere --->
       <cfset newTarget = replaceNoCase(arguments.targetPage, "/resources/img/prova.png", "/resources/#session.token#/img/prova.png")>
        <cflocation url="#newTarget#">
    </cffunction>

    Sorry, Fabio. This isn't quite right. Coldfusion will handle dynamic requests for files *.cfm, *.cfc, *.cfml, *.jsp, *.cfr, *.cfswf, and so on, but  will send requests to static files such as html, jpg, png, etc to the web server. It will indeed be foolish to then configure the web server to redelegate the png files back to Coldfusion.

    I felt that instinctively, but couldn't remember exactly. I should have heeded the response from Owain and Adam. To do a URL redirect for paths to static files *.png, *.html, *.jpg,  etc., you have to create mappings in the respective web server(IIS,  Apache, etc) configuration files.

    In any case, there is a solution that is in the spirit of the one I have given.  It is the solution that possibly was lingering at the back of my mind. Here, the static png files have been embedded, for example, using the img tag, in respective cfm files:

    <cffunction name="onRequestStart">
        <cfargument name = "targetPage" type="String" required="true">
        <!--- Assumes session.token = 002 has been set elsewhere --->
      
    <cfset newTarget = replaceNoCase(arguments.targetPage,
    "/resources/img/prova.cfm",
    "/resources/#session.token#/img/prova.cfm")>
        <cflocation url="#newTarget#">
    </cffunction>

    WolfShade
    Legend
    January 14, 2011

    Pretty much the way you already have it, if I understand your question correctly.

    You'd just want to numberformat the id to include the padded zeroes.

    <cfoutput>

    /resources/#NumberFormat(SESSION.shopId,'000')#/img/happy.png

    </cfoutput>

    Participant
    January 15, 2011

    No sorry... You didn't understand. It's not a simple number format issue.

    What I'd like is to intercept the http request for /resources/img/prova.png and redirect it to /resources/002/img/prova.png ...

    where 002 is my session variable. How can I do that, e.g. in the Application.cfc?

    Anyone one else can answer??

    Owainnorth
    Inspiring
    January 15, 2011

    Hmm, not sure it's possible using a session var. PNG files are not routed to ColdFusion, they are simply loaded from disk by the web server. In order for CF to even get the request, you'd have to use mod_rewrite (or platform equivalent) to rewrite the request.

    However, Session variables are stored within CF, and the web server has no visibility of them. The only thing I can immediately think of is to store the variable you need in a cookie in their browser, and then use mod_rewrite to read the cookie value and perform the redirect. This is not a job for ColdFusion.

    HTH.