0

いくつかのディレクトリ/パスを除いて、ほとんどすべてを別のドメインにリダイレクトする必要がある Web サイトがあります。サーバーは、ホスティング環境で ColdFusion と IIS を実行しています。

機能性:

a) http://example1.com redirects to http://example2.com
b) http://example1.com/special stays put
c) http://example1.com/anydir redirects to http://example2.com

これを達成する方法について何か提案はありますか?

ColdFusion で行うことを検討しましたが、これではケース c) が処理されません。これはホスティング プロバイダーの制限であるため、IIS での URL 書き換えはできません。

編集:

上記の機能がこのケースを明示的に述べていないことに気付きました:

d) http://example1.com/anydir/anydir redirects to http://example2.com
4

2 に答える 2

1

以前、既存のアプリケーションを古いパスから新しいパスにリダイレクトするためにこれを作成しました。たとえば、「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>
于 2011-12-02T14:17:12.973 に答える
0

可能であれば、サーバーでリダイレクトを処理する方が適切ですが、可能であれば、このようなもので CF を使用してそれを行うことができます..どのように構造化するかは、実際に処理する必要がある実際の URL によって異なります..

ケース (c) は正規表現で処理できる可能性があります。

<!---get the current URL--->
<cfset currentURL = "#cgi.server_name##cgi.path_info#" > 

<!---based on the URL, redirect accordingly--->
<cfif FindNoCase("example1.com/special", currentURL)>
    <!---do nothing--->
<cfelseif FindNoCase("example1.com", currentURL)>
    <cflocation url="http://www.example2.com" >
</cfif>
于 2011-12-01T22:58:40.993 に答える