【问题标题】:Redirect all web traffic except a few dirs/paths?重定向除少数目录/路径之外的所有网络流量?
【发布时间】:2011-12-01 22:33:51
【问题描述】:

我有一个网站,我需要将几乎所有内容重定向到另一个域,除了几个目录/路径。服务器在托管环境中运行 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

【问题讨论】:

    标签: iis coldfusion windows-server


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      如果可以的话,你最好用服务器处理重定向,但如果可以的话,你可以用 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>
      

      【讨论】:

      • 我试图在 Application.cfm 中添加一个变体,以便始终处理它,以确保所有请求都被转发,除了排除的请求。然而它没有用。 Application.cfm 不是合适的地方吗?
      • Application.cfm 是放置它的好地方。为了确保它正在运行,只需在块上方和下方输出一些标签,看看它们是否正在输出……尝试输出#currentURL# 看看它读到了什么......玩弄它......它应该可以工作......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2019-11-09
      • 2014-10-18
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多