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

how to create an txt file from a dynamic cfm page.

Explorer ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

I need to recreate the .htaccess file whenever the page structure changes.

I have wrapped the cf code with cfsavecontent, then write the cfsavecontent variable using cffile.

The results produce a file that just reproduces my cf code, not the net output of that code.

Any suggestions?

Views

2.1K

Translate

Translate

Report

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

Explorer , Apr 16, 2014 Apr 16, 2014

Thanks all for the help. Got it!  I needed to strip all the html tags and remove extraneous lines in my code.  Still needed cfoutput tags inside of cfsavecontent to generate the variables.

This is cool because now  the htaccess file will be re-created dynamically every time a client changes page structure.

P.S.  I hate SEO (search engine optimized) page names!

Here's the finished code:

<cfsavecontent variable="myhtaccess">

<cfoutput>

RewriteEngine on

RewriteRule index.htm index.cfm [I,O,R=301,L]

Rewrite

...

Votes

Translate

Translate
Community Expert ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

Why wrap the cf code, and not just the output?

Oh, and what is your ColdFusion version?

Votes

Translate

Translate

Report

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
Explorer ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

My file looks like this:

<cfsavecontent variable="myhtaccess">

<font face="courier new"><font size="2">

<b>****Copy, Save and Upload this file to replace '.htaccess' for Non Profit Dynamics.</b>

<br><br>

RewriteEngine on<br><br>

RewriteRule index.htm index.cfm [I,O,R=301,L]<br><br>

</cfsavecontent>

<cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess#" >

The output file simply records everything inside the cfsavecontent tags, but not the results of the cf code.

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

There is no cf code!

Votes

Translate

Translate

Report

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
Explorer ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

here is the full code:

<cfoutput>

<cfsavecontent variable="myhtaccess">

<font face="courier new"><font size="2">

<b>****Copy, Save and Upload this file to replace '.htaccess' for #getaccount.client#.</b>

<br><br>

RewriteEngine on<br><br>

RewriteRule index.htm index.cfm [I,O,R=301,L]<br><br>

RewriteCond %{HTTP_HOST} !^$<br>

RewriteCond %{HTTP_HOST} !^www\. [NC]<br>

RewriteCond %{HTTPS}s ^on(s)|<br>

RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<br><br>

RewriteCond %{REQUEST_FILENAME} !-f<br>

RewriteCond %{REQUEST_FILENAME} !-d<br>

RewriteRule ^home index.cfm [NC,L,QSA]<br><br>

<CFQUERY

Name="getmenu"

Datasource="#db.02.dsn#"  >

SELECT

*

FROM page

order by pageid

</CFQUERY>

<div class="bcopy11">

<cfloop query="getmenu" startrow="1" endrow="#getmenu.recordcount#">

<cfif getmenu.pageid neq 20>

<cfif getmenu.pageid neq 21>

<cfif getmenu.pageid neq 22>

<cfif getmenu.pageid neq 23>

<cfif getmenu.pageid neq 28>

<cfif getmenu.pageid neq 29>

<cfif getmenu.pageid neq 30>

<cfif getmenu.pageid neq 31>

<CFQUERY

Name="getmenusub"

Datasource="#db.02.dsn#"  >

SELECT

*

FROM pagesub

where pageid = #getmenu.pageid#

</CFQUERY>

<cfif getmenu.pagename contains " ">

<cfset menu = ReReplace(getmenu.pagename, " ", "\ ", "ALL")>

<cfelse>

<cfset menu = getmenu.pagename>

</cfif>

RewriteRule ^#menu# page.cfm?pageid=#getmenu.pageid# [NC,L,QSA]<br>

    <!start level 2>

    <cfloop query="getmenusub" startrow="1" endrow="#getmenusub.recordcount#">

    <CFQUERY

    Name="getmenusub2"

    Datasource="#db.02.dsn#"  >

    SELECT

     *

    FROM pagesub2

    where pagesubid = #getmenusub.pagesubid#

    </CFQUERY>

<cfif getmenusub.pagename contains " ">

<cfset menusub = ReReplace(getmenusub.pagename, " ", "\ ", "ALL")>

<cfelse>

<cfset menusub = getmenusub.pagename>

</cfif>

RewriteRule ^#menusub# pagesub.cfm?pagesubid=#getmenusub.pagesubid# [NC,L,QSA]<br>

        <!start level 3>

        <cfloop query="getmenusub2" startrow="1" endrow="#getmenusub2.recordcount#">

        <CFQUERY

        Name="getmenusub3"

        Datasource="#db.02.dsn#"  >

        SELECT

         *

        FROM pagesub3

        where pagesub2id = #getmenusub2.pagesub2id#

        </CFQUERY>

<cfif getmenusub2.pagename contains " ">

<cfset menusub2 = ReReplace(getmenusub2.pagename, " ", "\ ", "ALL")>

<cfelse>

<cfset menusub2 = getmenusub2.pagename>

</cfif>

RewriteRule ^#menusub2# pagesub2.cfm?pagesub2id=#getmenusub2.pagesub2id# [NC,L,QSA]<br>

            <!start level 4>

            <cfloop query="getmenusub3" startrow="1" endrow="#getmenusub3.recordcount#">

            <CFQUERY

            Name="getmenusub4"

            Datasource="#db.02.dsn#"  >

            SELECT

             *

            FROM pagesub3

            where pagesub3id = #getmenusub3.pagesub3id#

            </CFQUERY>

<cfif getmenusub3.pagename contains " ">

<cfset menusub3 = ReReplace(getmenusub3.pagename, " ", "\ ", "ALL")>

<cfelse>

<cfset menusub3 = getmenusub3.pagename>

</cfif>

RewriteRule ^#menusub3# pagesub3.cfm?pagesub3id=#getmenusub3.pagesub3id# [NC,L,QSA]<br>

            </cfloop>

            <!end level 4>

        </cfloop>

        <!end level 3>

    </cfloop>

    </ul>

    <!end level 2>

<!end level 1>

</cfif>

</cfif>

</cfif>

</cfif>

</cfif>

</cfif>

</cfif>

</cfif>

</cfloop>

</cfsavecontent>

#myhtaccess#

<cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess#" >

</cfoutput>

Votes

Translate

Translate

Report

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
Explorer ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

the output from that code should produce this:

****Copy, Save and Upload this file to replace '.htaccess' for clientname.

  RewriteEngine on

  RewriteRule index.htm index.cfm [I,O,R=301,L]

  RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home index.cfm [NC,L,QSA]

   

    RewriteRule ^Home page.cfm?pageid=1 [NC,L,QSA]
             RewriteRule ^Surveys page.cfm?pageid=2 [NC,L,QSA]
             RewriteRule ^Services page.cfm?pageid=3 [NC,L,QSA]
       RewriteRule ^Program\ Costs pagesub.cfm?pagesubid=31 [NC,L,QSA]
                  RewriteRule ^Clients page.cfm?pageid=4 [NC,L,QSA]
       RewriteRule ^Classic-Car\ Auctions pagesub.cfm?pagesubid=36 [NC,L,QSA]
            RewriteRule ^ECCO pagesub.cfm?pagesubid=37 [NC,L,QSA]
            RewriteRule ^St.\ Peter\ in\ Chains pagesub.cfm?pagesubid=38 [NC,L,QSA]
            RewriteRule ^Operation\ R\ &\ R pagesub.cfm?pagesubid=39 [NC,L,QSA]
            RewriteRule ^Respite\ Care\ Charleston pagesub.cfm?pagesubid=40 [NC,L,QSA]
            RewriteRule ^Pilgrim\ UCC pagesub.cfm?pagesubid=41 [NC,L,QSA]
            RewriteRule ^The\ CARES\ Clinic pagesub.cfm?pagesubid=42 [NC,L,QSA]
            RewriteRule ^Courageous\ Kidz pagesub.cfm?pagesubid=43 [NC,L,QSA]
                  RewriteRule ^Testimonials page.cfm?pageid=5 [NC,L,QSA]
       RewriteRule ^Lakeview\ UCC pagesub.cfm?pagesubid=32 [NC,L,QSA]
            RewriteRule ^St.\ Peters pagesub.cfm?pagesubid=44 [NC,L,QSA]
                  RewriteRule ^About\ NPD page.cfm?pageid=6 [NC,L,QSA]
             RewriteRule ^Contact\ Us page.cfm?pageid=7 [NC,L,QSA]
             RewriteRule ^Donate page.cfm?pageid=24 [NC,L,QSA]
             RewriteRule ^Privacy\ Policy page.cfm?pageid=25 [NC,L,QSA]
             RewriteRule ^Photos page.cfm?pageid=26 [NC,L,QSA]
             RewriteRule ^Change\ Font\ Size page.cfm?pageid=27 [NC,L,QSA]
             RewriteRule ^Register page.cfm?pageid=32 [NC,L,QSA]

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

I see what you mean. First things first, the outermost cfoutput tags are unnecessary. I should also like to add to what WolfShade has said.

The tags <br>, <font> and <b> are HTML. They are usually to be rendered by the browser. So, it makes sense for them to be in the output only if you intend the final text to be displayed in a browser or some other HTML reader. So, to be consistent, you have to open the saved file in a browser, not in a text editor!

If you wish to use the end result as a functional .htaccess file, then you have to remove the HTML tags. However, nil desperandum! There are still possibilities. For example, to ColdFusion, <br> is just a string. What ColdFusion understands as a linefeed/carriage-return is #chr(10)##chr(13)#.

I asked for your ColdFusion version because my suggestion is to use in-memory files. This requires the following setting in the Mappings page in the ColdFusion Administrator:

Logical Path: /inMemory

Directory Path: ram:///

You could then proceed as follows:

<cfsavecontent variable="myhtaccess_cfml">

etc.

</cfsavecontent>

<!--- Write to file in memory --->

<cffile action="write" output="#myhtaccess_cfml#" file="ram:///myhtaccessFile.cfm"/>

<!--- Execute in-memory CFM file. Remember the mapping is called 'inMemory' --->

<cfsavecontent variable="myhtaccess_output">

    <cfinclude template="/inMemory/myhtaccessFile.cfm">

</cfsavecontent>

<!--- Write output to real file --->

<cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess_output#" >

Votes

Translate

Translate

Report

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
Engaged ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

I really don't understand this response.  This adds tons of complexity to a solution that doesn't need it.  You could easily just do a savecontent and then write that result to a file. 

BKBK wrote:

.....

<cfsavecontent variable="myhtaccess_cfml">

etc.

</cfsavecontent>

<!--- Write to file in memory --->

<cffile action="write" output="#myhtaccess_cfml#" file="ram:///myhtaccessFile.cfm"/>

<!--- Execute in-memory CFM file. Remember the mapping is called 'inMemory' --->

<cfsavecontent variable="myhtaccess_output">

    <cfinclude template="/inMemory/myhtaccessFile.cfm">

</cfsavecontent>

<!--- Write output to real file --->

<cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess_output#" >

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

fergusondj wrote:

I really don't understand this response.  This adds tons of complexity to a solution that doesn't need it.  You could easily just do a savecontent and then write that result to a file. 

Quite right. When you look back you can see I said pretty much the same thing, and mentioned where in-memory file processing would be suitable. Our posts seem to have crossed on the wire.

Votes

Translate

Translate

Report

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
Explorer ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

As does it usually happen, the solution turns out to be much more simple and elegant than first anticipated!

Thanks again!

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

LATEST

@RaehmLeonard

Please mark your answer as 'correct'. It will help someone else in future.

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

The inmemory (VFS) is a great idea as long as the CF version is 10+.  Earlier versions do not have a "per application" VFS, allowing any site/app on that server to access any file in the VFS.

Just my $0.032749 worth.

^_^

Votes

Translate

Translate

Report

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
Engaged ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

VFS is a great idea when using it makes sense.  However, using it when it will overcomplicate a solution doesn't.

WolfShade wrote:

The inmemory (VFS) is a great idea as long as the CF version is 10+.  Earlier versions do not have a "per application" VFS, allowing any site/app on that server to access any file in the VFS.

Just my $0.032749 worth.

^_^

Votes

Translate

Translate

Report

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
Explorer ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

Thanks all for the help. Got it!  I needed to strip all the html tags and remove extraneous lines in my code.  Still needed cfoutput tags inside of cfsavecontent to generate the variables.

This is cool because now  the htaccess file will be re-created dynamically every time a client changes page structure.

P.S.  I hate SEO (search engine optimized) page names!

Here's the finished code:

<cfsavecontent variable="myhtaccess">

<cfoutput>

RewriteEngine on

RewriteRule index.htm index.cfm [I,O,R=301,L]

RewriteCond %{HTTP_HOST} !^$

RewriteCond %{HTTP_HOST} !^www\. [NC]

RewriteCond %{HTTPS}s ^on(s)|

RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^home index.cfm [NC,L,QSA]

<CFQUERY

Name="getmenu"

Datasource="#db.02.dsn#"  >

SELECT

*

FROM page

order by pageid

</CFQUERY>

<cfloop query="getmenu" startrow="1" endrow="#getmenu.recordcount#">

<CFQUERY

Name="getmenusub"

Datasource="#db.02.dsn#"  >

SELECT

*

FROM pagesub

where pageid = #getmenu.pageid#

</CFQUERY>

<cfif getmenu.pagename contains " ">

<cfset menu = ReReplace(getmenu.pagename, " ", "\ ", "ALL")>

<cfelse>

<cfset menu = getmenu.pagename>

</cfif>

RewriteRule ^#menu# page.cfm?pageid=#getmenu.pageid# [NC,L,QSA]

    <cfloop query="getmenusub" startrow="1" endrow="#getmenusub.recordcount#">

    <CFQUERY

    Name="getmenusub2"

    Datasource="#db.02.dsn#"  >

    SELECT

     *

    FROM pagesub2

    where pagesubid = #getmenusub.pagesubid#

    </CFQUERY>

<cfif getmenusub.pagename contains " ">

<cfset menusub = ReReplace(getmenusub.pagename, " ", "\ ", "ALL")>

<cfelse>

<cfset menusub = getmenusub.pagename>

</cfif>

RewriteRule ^#menusub# pagesub.cfm?pagesubid=#getmenusub.pagesubid# [NC,L,QSA]

        <cfloop query="getmenusub2" startrow="1" endrow="#getmenusub2.recordcount#">

        <CFQUERY

        Name="getmenusub3"

        Datasource="#db.02.dsn#"  >

        SELECT

         *

        FROM pagesub3

        where pagesub2id = #getmenusub2.pagesub2id#

        </CFQUERY>

<cfif getmenusub2.pagename contains " ">

<cfset menusub2 = ReReplace(getmenusub2.pagename, " ", "\ ", "ALL")>

<cfelse>

<cfset menusub2 = getmenusub2.pagename>

</cfif>

RewriteRule ^#menusub2# pagesub2.cfm?pagesub2id=#getmenusub2.pagesub2id# [NC,L,QSA]

            <cfloop query="getmenusub3" startrow="1" endrow="#getmenusub3.recordcount#">

            <CFQUERY

            Name="getmenusub4"

            Datasource="#db.02.dsn#"  >

            SELECT

             *

            FROM pagesub3

            where pagesub3id = #getmenusub3.pagesub3id#

            </CFQUERY>

<cfif getmenusub3.pagename contains " ">

<cfset menusub3 = ReReplace(getmenusub3.pagename, " ", "\ ", "ALL")>

<cfelse>

<cfset menusub3 = getmenusub3.pagename>

</cfif>

RewriteRule ^#menusub3# pagesub3.cfm?pagesub3id=#getmenusub3.pagesub3id# [NC,L,QSA]

            </cfloop>

        </cfloop>

    </cfloop>

</cfloop>

</cfoutput>

</cfsavecontent>

done!

<cffile action="write" file="#getaccount.subsite#/adm/htaccess.txt" output="#myhtaccess#" charset="utf-8">

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

RaehmLeonard wrote:

Thanks all for the help. Got it!

Good music!

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

WolfShade wrote:

The inmemory (VFS) is a great idea as long as the CF version is 10+. 

CF 9 had it too.

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

I know CF9 had it too.. it just wasn't "per application", allowing any app on the server to access any file.  The benefit to 10 is the per application preventing apps from accessing other VFS files that belong to other apps.

^_^

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

I should hurry to add that the method I have just given makes sense only when the output of the first cfsavecontent is CFML code! Otherwise, you should just use the original, simpler version, namely:

<cfsavecontent variable="myhtaccess">

etc.

</cfsavecontent>

<!--- Write output to real file --->

<cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess#" >

Votes

Translate

Translate

Report

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 ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

<cfsavecontent variable="myhtaccess">

DirectoryIndex index.php index.html index.htm

php_value memory_limit 40M

php_flag magic_quotes_gpc 1

php_flag magic_quotes_runtime 0

php_flag file_uploads 1

php_flag session.auto_start 0

php_flag session.bug_compat_warn 0

</cfsavecontent>

<cffile action="write" file="#getaccount.subsite#/adm/.htaccess" output="#myhtaccess#" >

.. should do it.  Why the font tag?  <br />?  <b>?  I didn't think these were supposed to be in a .htaccess file.

^_^

Votes

Translate

Translate

Report

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
Documentation