Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

URL rewriting trick

New Here ,
Jan 13, 2011 Jan 13, 2011

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?

1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 16, 2011 Jan 16, 2011

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

...
Translate
LEGEND ,
Jan 14, 2011 Jan 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 15, 2011 Jan 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??

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jan 15, 2011 Jan 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 15, 2011 Jan 15, 2011

Agreed

However I'd probably not take that approach either, despite the requirement being "without changing existing pages", because I think it's a pretty crap approach to solving the problem.

If it's just a matter of theming, I'd do it all with CSS and load different CSS files depending on which "shop" it is.

Or I'd revise the asset URLs in the mark-up to reflect the correct path in the first place.

Having the "wrong" URL path and then trying to fix it with rewrites is just daft.  Get it right to start with.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 16, 2011 Jan 16, 2011

If it's just a matter of theming, I'd do it all with CSS and load different CSS files depending on which "shop" it is.


I'm pleased with it but... what does that mean? Can you code a couple of row example?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 16, 2011 Jan 16, 2011

Fabio, kind of you. However, I decline the points just yet. Please remove the 10 points.

Let me first recreate a working example. For the moment, my CF9/Eclipse installation has gone coocoo. I'm busy with it, and will hopefully get back to this shortly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 16, 2011 Jan 16, 2011

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 16, 2011 Jan 16, 2011

But requests to PNG files are dealt with via the web server... that code would never be executed.

Or are you advocating reconfiguring the web server connector to pass PNG (etc) requests through to CF as well, just so as to deal with the URL?  Bleah.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 16, 2011 Jan 16, 2011

Adam Cameron. wrote:

But requests to PNG files are dealt with via the web server... that code would never be executed.

Or are you advocating reconfiguring the web server connector to pass PNG (etc) requests through to CF as well, just so as to deal with the URL?

I am, aint I. Let me first repair my installation, and then I'll see if I can come up with a proof of concept for mapping static files in CF9.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 16, 2011 Jan 16, 2011
LATEST
<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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources