假设文件.htaccess.template 是这样的:
# BEFORE
# --- Start Part1 ---#
# --- End Part1 ---#
# AFTER
在同一目录中有一个 ColdFusion 脚本,比如 htaccess.cfm(名称无关紧要):
<!--- note double pound signs, necessary to escape in CF --->
<cfset start = "## --- Start Part1 ---##">
<cfset end = "## --- End Part1 ---##">
<cfsavecontent variable="replacement"><cfoutput>
I will appear between the start and end comments!
Replace me with what you want to appear in the .htaccess file.
</cfoutput></cfsavecontent>
<cfset template = fileRead(getDirectoryFromPath(getCurrentTemplatePath()) & "/.htaccess.template")>
<cfset startPos = find(start, template)>
<cfset endPos = find(end, template)>
<cfset before = left(template, startPos + len(start) - 1)>
<cfset after = right(template, len(template) - endPos + 1)>
<cfset content = "#before##replacement##after#">
<!--- <cfoutput><pre>#content#</pre></cfoutput> --->
<cfset path = getDirectoryFromPath(getCurrentTemplatePath()) & "/.htaccess">
<cfif fileExists(path)><cfset fileDelete(path)></cfif>
<cfset fileWrite(path, content)>
这将在同一目录中生成一个文件.htaccess。我认为一个问题将是处理放置在.htaccess 上的任何文件系统锁,以防止删除/覆盖,我不确定在这种情况下您需要做什么。
在本例中,.htaccess 将是:
# BEFORE
# --- Start Part1 ---#
I will appear between the start and end comments!
Replace me with what you want to appear in the .htaccess file.
# --- End Part1 ---#
# AFTER