2011-12-01 70 views
0

我有一個網站,我需要重定向幾乎一切到另一個域,除了幾個目錄/路徑。服務器在託管環境中運行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 

回答

0

你,如果你能更好地處理與重定向服務器,但是如果可以的話你可以用CF做這樣的事情..你的結構真的取決於你需要處理的實際URL是什麼......

你可能會用正則表達式來處理case(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> 
+0

我試圖添加一個差來在Application.cfm上進行處理,以便始終處理它,以確保所有請求都被轉發,除了被排除的請求之外。但它沒有奏效。 Application.cfm不是正確的地方嗎? – noobzie

+0

Application.cfm是一個放置它的好地方..要確保它正在運行,只需在塊的上下輸出一些標籤以查看它們是否正在輸出....嘗試輸出#currentURL#以查看內容它讀..玩它..它應該工作.. – Jason

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"}    
    ] 
} /> 

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> 
相關問題