8 Replies Latest reply on Jan 19, 2010 5:41 AM by Adam Cameron.

    Apache mod_rewrite CGI Variables in ColdFusion

    dmalloy

      I have an environment that consists of Apache 2.2.8 and ColdFusion 8 running on Solaris 10.

       

      I am attempting to pass a CGI variable to ColdFusion as part of an Apache Rewrite rule, however I am not able to do so.

       

      The Rewrite rule set exists as follows:

       

      RewriteCond %{SSL:SSL_PROTOCOL} ^TLSv1$
      RewriteCond %{HTTP_REFERER} !^$ [NC]
      RewriteRule ^.*$ %{HTTP_REFERER}?result=pass [L,R,E=HTTP_MYVAR:rw]

       

      The rule set works with the exception of the availability of the CGI variable HTTP_MYVAR within the ColdFusion CGI scope.

       

      I am testing for the existance of the variable within ColdFusion as follows:

       

      <cfif (structkeyexists(cgi, 'CGI.HTTP_MYVAR'))>
         CGI.HTTP_MYVAR exists as:<cfoutput>#CGI.HTTP_MYVAR#</cfoutput>
      <cfelse>
         CGI.HTTP_MYVAR does not exist.
      </cfif>

       

      Does anyone have experience creating variables in an Apache Rewrite rule set and evaluating them in ColdFusion?

       

      Thanks in advance for any assistance.

        • 1. Re: Apache mod_rewrite CGI Variables in ColdFusion
          Adam Cameron. Level 5

          Does anyone have experience creating variables in an Apache Rewrite rule set and evaluating them in ColdFusion?

           

           

          I didn't until trying to investigate this issue for you, but do now.  So cheers for that! :-)

           

          Your problem, though, is nothing to do with MOD_REWRITE.  Have a look at this line of code:

           

          <cfif (structkeyexists(cgi, 'CGI.HTTP_MYVAR'))>

           

          Take a good close look at it...

           

          --

          Adam

          • 2. Re: Apache mod_rewrite CGI Variables in ColdFusion
            dmalloy Level 1

            Adam,

             

            Thank you taking the time to look at this issue.

             

            I have spent some time on this and I have not been successful in setting a variable in the Apache Rewrite rule and evaluating it within ColdFusion code.

             

            Can you send me sample RewriteRule code and ColdFusion code if you have been successful in accomplishing this?

             

            Thanks again.

            Dave

            • 3. Re: Apache mod_rewrite CGI Variables in ColdFusion
              Adam Cameron. Level 5

              Did you do what I suggested and have a good hard look at that CF code you posted?  And did you notice the mistake in it?

               

              Your Apache settings are fine.  It's your CF code that's got a (reasonably obvious, once you look at it) mistake in it.

               

              Think about the two parameters that structKeyExists() takes.  Have you got them right?  (hint: no).

               

              --

              Adam

              • 4. Re: Apache mod_rewrite CGI Variables in ColdFusion
                dmalloy Level 1

                structkeyexists() takes two parameters; structure and key.

                 

                My original code appeared as follows:

                 

                <cfif (structkeyexists(cgi, 'CGI.HTTP_MYVAR'))>

                ....

                 

                I have since modified it to:

                 

                <cfif (struckeyexists(cgi, 'HTTP_MYVAR'))>

                ....

                 

                and do not receive any different results.

                 

                I'm affraid I'm not seeing the error that you are suggesting exists.

                • 5. Re: Apache mod_rewrite CGI Variables in ColdFusion
                  Adam Cameron. Level 5

                  The thing that you fixed was the problem I was hinting at.

                   

                  I'm afraid the computer I did the testing of this on is sitting in New Zealand, and I'm currently in the UK, so what I did is not to hand sorry.  I'll be back in NZ on the w/end though, so I'll dig it out then.

                   

                  However my recollection is that I pretty much used your re-write code (simplified for a less specific situation), and it just worked.

                   

                  If I find some time tomorrow, I'll try to replicate it again, and feed back.

                   

                  --

                  Adam

                  • 6. Re: Apache mod_rewrite CGI Variables in ColdFusion
                    Adam Cameron. Level 5

                    OK, as part of my work-avoidance for the day, I have replicated the work I did in NZ on my other laptop.

                     

                    I put this in httpd.conf

                     

                    Alias /junk C:/webroots/junk
                    <Directory "C:/webroots/junk">
                        Options Indexes FollowSymLinks
                        AllowOverride None
                        Order allow,deny
                        Allow from all
                       
                        RewriteEngine  on
                        RewriteBase    /junk/
                        RewriteRule (.*) $1 [E=MYCUSTOMVARIABLE:mycustomvalue]

                    </Directory>

                     

                    And my test code is this:

                     

                    <cfoutput>
                    CGI.MYCUSTOMVARIABLE = [#CGI.MYCUSTOMVARIABLE#]<br />
                    </cfoutput>

                     

                    I restart Apache, run the template, and I get this:

                     

                    CGI.MYCUSTOMVARIABLE = [mycustomvalue]

                     

                    How does this differ from what you're doing?

                     

                    --

                    Adam

                    • 7. Re: Apache mod_rewrite CGI Variables in ColdFusion
                      dmalloy Level 1

                      Thanks again for you help.

                       

                      I modified my rewrite code as follows:

                       

                           #rewrite to a failure page if not TLSv1
                           RewriteCond %{SSL:SSL_PROTOCOL} !^TLSv1$
                           RewriteCond %{HTTP_REFERER} ^$ [NC]
                           RewriteRule ^.*$ http://mysite.mydomain.com?tlsresult-fail [L,R,E=MYCUSTOMVARIABLE:mycustomvalue]

                       

                      My ColdFusion code on the index page of the site includes:

                       

                               <cfdump var="#CGI#">
                              <cfoutput>
                                      CGI.MYCUSTOMVARIABLE = [#CGI.MYCUSTOMVARIABLE#]<br />
                               </cfoutput>          

                      The CGI variable CGI.MYCUSTOMVARIABLE does not appear in the CGI scope dump and the result of the <cfoutput> tag is:

                      CGI.MYCUSTOMVARIABLE=[]

                       

                      I know that the RewriteRule is working for everything except the creation of the CGI variable.

                       

                      Do you have any ideas what may be wrong?

                      • 8. Re: Apache mod_rewrite CGI Variables in ColdFusion
                        Adam Cameron. Level 5

                        <cfdump> will only dump out a subset of common CGI variables for some reason.  When quizzed, someone from Adobe suggested it's because to get any specific ones, it's a call to the web server or something, and there's overhead.  Sounded like a cop-out or the result of ill-thought-out code to me, but there you go.  This is also part of the "logic" as to why one can go isDefined("CGI.anythingCanGoHere") and it will always return true.  Stupid.

                         

                        As for why it's not showing up when accessed directly, I presume it's because your conditions aren't being met.

                         

                        Remove both of them and re-test, and work from there.

                         

                        --

                        Adam