4

I've written a brief CFscript to encode/decode links:

    function link_encode(str) {
    x = Replace(str, "&", "and", "ALL");
    y = Replace(x, " ", "_", "ALL");
    z = Replace(y, "/", "|", "ALL");
    return z;
}

function link_decode(str) {
    x = Replace(str, "and", "&", "ALL");
    y = Replace(x, "_", " ", "ALL");
    z = Replace(y, "|", "/", "ALL");
    return z;
}

This is in a file which is include in the site header (included on every page)

<cfinclude template="/includes/cfScriptFunctions.cfm">

This works fine in a 'normal' page:

<cfset link = link_encode(sub_menu.name)>

But, for SEO purposes I rewrite URLs via web.config:

<rule name="categories_1" stopProcessing="true">
<match url="category1/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/category1.cfm?id={R:1}" />
</rule>

When I land on this page, Coldfusion returns the error : "Variable LINK_DECODE is undefined". But, if I try to include the cfscript file in the page Coldfusion returns the error: "Routines cannot be declared more than once. The routine link_decode has been declared twice in different templates.". This tells me that the routine is available so why isn't it being found ?

update Ooops... My Fault ... I was calling the function before it had been included in the page ... Duh.

Pat Dobson
  • 3,249
  • 2
  • 19
  • 32
  • Are you sure it's not an issue of calling `link_encode` when you mean `link_decode` or vise versa? – Kyle Macey Jan 29 '14 at 22:26
  • Dead sure... I've edited my original post to include both encode/decode functions... Neither are found after the URL rewrite. – Pat Dobson Jan 29 '14 at 22:28
  • Can you paste the line that the error references exactly? – Kyle Macey Jan 29 '14 at 22:30
  • - where URL.id has been encoded with the 'link_encode' routine. ( ) – Pat Dobson Jan 29 '14 at 22:33
  • When you run it in debug mode, does it say that `/includes/cfscriptFunctions.cfm` was included? – James A Mohler Jan 29 '14 at 22:34
  • Debugging isn't available to me - I'm on a shared server. – Pat Dobson Jan 29 '14 at 22:38
  • However, I simply put the block directly in the header include - still didn't work ! – Pat Dobson Jan 29 '14 at 22:40
  • interestingly, if I remove the web.config rewriting and go directly to the page, it throws the same error . . . – Pat Dobson Jan 29 '14 at 22:41
  • Even more interesting, I've come across this before (and not resolved it). In the past I've included the specifically on each page where I've wanted it. (I've only just remembered that !). There's got to be a better way ! – Pat Dobson Jan 29 '14 at 22:49
  • 1
    @PatDobson your production site might be on a shared server, but surely your dev environment isn't?? – Adam Cameron Jan 29 '14 at 23:39
  • Also suggest removing the reference to URL rewrites if they're not relevant (which you seem to have established they're not). – Adam Cameron Jan 29 '14 at 23:45
  • @PatDobson - This has nothing to do with your current issue, but do not forget to var scope the function local variables `x,y,z`. – Leigh Jan 30 '14 at 14:50
  • @Leigh - Thanks for that - I've always had problems with the concepts of 'scope' :) – Pat Dobson Jan 30 '14 at 15:40
  • Question... on your cfml page... when are you calling the link_decode? When are you including the header file? I've seen people call the link decode above their headers, to get queries built etc, to put data into the header, but the header has the cfscript. Check your flow, incase you're calling it before the include. – Gavin Pickin Jan 30 '14 at 17:46
  • @Gavin Pickin - You're a genius ! I'd completely overlooked that ! Please convert your comment into an answer so I can give you the credit you deserve ! – Pat Dobson Jan 30 '14 at 18:12
  • Converted. Its a common mistake... thats when a good framework is handy for loading items in the right order. Glad we could help. – Gavin Pickin Jan 30 '14 at 19:07

1 Answers1

1

Question... on your cfml page... when are you calling the link_decode? When are you including the header file? I've seen people call the link decode above their headers, to get queries built etc, to put data into the header, but the header has the cfscript. Check your flow, incase you're calling it before the include.

Gavin Pickin
  • 742
  • 3
  • 7