Skip to main content
Inspiring
April 16, 2014
Answered

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

  • April 16, 2014
  • 1 reply
  • 2684 views

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?

This topic has been closed for replies.
Correct answer RaehmLeonard

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.

^_^


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">

1 reply

BKBK
Community Expert
Community Expert
April 16, 2014

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

Oh, and what is your ColdFusion version?

Inspiring
April 16, 2014

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.

BKBK
Community Expert
Community Expert
April 16, 2014

There is no cf code!