我創建了一段時間,將現有應用程序從舊路徑重定向到新路徑。我相信它依賴子文件夾存在,例如「anydir/anydir /」實際上必須是真實的文件夾。我基本上只是將其粘貼到現有的應用程序文件夾中,以便配置,應用程序和索引文件被覆蓋,然後根據config中的定義進行重定向。
重定向的定義是正則表達式,所以如果有必要,實際上可能會變得非常複雜。它是一個有序數組,因此您可以首先放置更具體的重定向,最後放置更一般的重定向。如果沒有定義匹配,你可以在最後包含「最後的手段」重定向或允許發生錯誤 - 這取決於你想要的精確程度。
配置/ config.cfm
<cfset config = {
debug=true
, redirects = [
{find="^/path/temp/dir2/(.+)$", replace="http://temp.domain.com/dir2\1"}
, {find="^/path/temp/(.+)$", replace="http://temp.domain.com/\1"}
]
} />
index.cfm
[blank file]
的Application.cfc
<cfcomponent>
<cfset this.name="Redirect#hash(getCurrentTemplatePath())#"/>
<cfinclude template="config/config.cfm" />
<cffunction name="onRequestStart">
<cfset redirect(cgi.path_info) />
</cffunction>
<cffunction name="onMissingTemplate">
<cfargument name="targetPage" required="true" />
<cfset redirect(arguments.targetPage) />
</cffunction>
<cffunction name="redirect">
<cfargument name="targetPage" required="true" />
<cfset var i = 0 />
<cfset var newpath = "" />
<cfloop from="1" to="#arraylen(variables.config.redirects)#" index="i">
<cfif refindnocase(variables.config.redirects[i].find, arguments.targetPage)>
<cfset newpath = rereplacenocase(arguments.targetPage, variables.config.redirects[i].find, variables.config.redirects[i].replace) />
<cfif len(cgi.query_string)>
<cfset newpath &= "?" & cgi.query_string />
</cfif>
<cfif variables.config.debug>
<cfoutput>#newpath#</cfoutput>
<cfabort>
</cfif>
<cflocation url="#newpath#" addtoken="false" />
</cfif>
</cfloop>
<cfthrow type="custom.redirect.notfound" />
<cfabort>
</cffunction>
</cfcomponent>
我試圖添加一個差來在Application.cfm上進行處理,以便始終處理它,以確保所有請求都被轉發,除了被排除的請求之外。但它沒有奏效。 Application.cfm不是正確的地方嗎? – noobzie
Application.cfm是一個放置它的好地方..要確保它正在運行,只需在塊的上下輸出一些標籤以查看它們是否正在輸出....嘗試輸出#currentURL#以查看內容它讀..玩它..它應該工作.. – Jason