以前、既存のアプリケーションを古いパスから新しいパスにリダイレクトするためにこれを作成しました。たとえば、「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"}
]
} />
索引.cfm
[blank file]
アプリケーション.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>