【问题标题】:URL Rewrite with IIS using ColdFusion使用 ColdFusion 使用 IIS 重写 URL
【发布时间】:2015-10-09 17:40:40
【问题描述】:

我进行了一些搜索,但无法将所有内容放在一起。这是我需要的:

我想找人上网:

www.mysite.com/thisPlace

并让它重定向到

www.mysite.com/template.cfm?pm=ms&loc_id=4

为此,我需要以某种方式捕获他们没有在其 http 请求中请求现有文件并运行一个 .cfm 页面,该页面在数据库中查询 locationName = 'thisPlace' 的记录,然后将它们重定向到页面喜欢 template.cfm?pm=ms&loc_id=4,其中 4 是匹配 'thisPlace' 的行的记录 id

【问题讨论】:

  • 为什么用PHP标记?
  • 不知道,我提交的时候没看到。抱歉,我删除了它。

标签: coldfusion url-rewriting iis-7.5 coldfusion-11


【解决方案1】:

如果您在 IIS 中的默认文档设置为 index.cfm,您可以创建一个名为“thisPlace”的文件夹(目录)并放置一个 index.cfm 文件,其中只包含一个 <cflocation> 标记和随附的查询/逻辑找出网址。

Website.com/thisPlace 将按照您的描述运行。

编辑:

您可以添加自定义 404 页面...

将其设为 .cfm 文件而不是 html。扫描模板路径以查看用户正在查找的内容。如果您在数据库中找到它,请将它们重定向到那里,否则将它们重定向到一般的 404 页面。

<!---Up to a certain point (the directory in which you store your code) this will always be the same so you can hard-code your number --->
<cfset QueryConstant = #LEFT(CGI.CF_Template_Path, 22)#>
<!---Find the overall length of the template path. --->
<cfset QueryVariable = #Len(CGI.CF_Template_Path)#>
<!---Take whatever is past your QueryConstant (AKA the string that produces a 404 error.) --->
<cfset theRightNumber = QueryVariable - 22>
<cfset QuerySearchString = #RIGHT(CGI.CF_Template_Path, theRightNumber)#>



<cfquery name="ListOfLocations" datasource="CRM">
    SELECT TOP 1 LocationID
    FROM LocationTable
    WHERE LocationName LIKE '%#QuerySearchString#%'
</cfquery> 

<cfif ListOfLocations.recordcount>
<cflocation url="/SomePage.cfm?LocationID=#ListOfLocations.LocationID#">
<cfelse>
<cflocation url="/Regular404page.html">
</cfif>

【讨论】:

  • 这可行,但是有超过 300 个独特的位置(例如“thisPlace”)。我希望有更通用的东西,不需要物理文件夹。
  • @Bill_VA 编辑了我的答案。我只是在运行中编写了代码,但它应该适用于你正在做的事情。
  • 我有类似的东西,但在 apache 上,但我使用 404.cfm 中的 CGI.REDIRECT_URL,它为我提供了 mysite.com 之后的所有内容。我还将 CGI.REDIRECT_URL 放入 cfqueryparam
  • 另外,您可能希望在 cflocation 中包含一个 STATUSCODE="301" 用于永久重定向,而 STATUSCODE="404" 用于正常 404
  • @luke 是正确的。添加状态码和 urltokens 可能非常有帮助,但最终该决定取决于 OP。不过很好。
【解决方案2】:

谢谢各位!巨大的帮助!使用您的输入,这就是我所做的: (必须使用 QUERY_STRING 而不是 CF_Template_Path,因为 CF_Template_Path 在自定义错误页面的 url 之后没有传递任何内容。

我在 IIS 中设置了一个自定义的 404 错误执行 URL 到一个名为 check404error.cfm 的文件。

当有人查找 www.example.com/thisPlace 时,IIS 将他们发送到 http://www.example.com/check404error.cfm。我使用 CGI.QUERY_STRING (404;http://www.example.com:443/thisPlace) 最终获得要搜索的“thisPlace”字符串。

<!---Up to a certain point (the directory in which you store your code) this will always be the same so you can hard-code your number --->
<cfset QueryConstant = #LEFT(CGI.QUERY_STRING, 31)#>
<!---Find the overall length of the template path. --->
<!---31 is the length of '404;http://www.example.com:443/' --->
<cfset QueryVariable = #Len(CGI.QUERY_STRING)#>
<!---Take whatever is past your QueryConstant (AKA the string that produces a 404 error.) --->
<cfset theRightNumber = QueryVariable - 31>
<cfset QuerySearchString = #RIGHT(CGI.QUERY_STRING, theRightNumber)#>

<cfquery name="ListOfLocations"  datasource="#request.dsn#">
 	SELECT location.id
	FROM location WHERE url_name = <cfqueryparam value="#QuerySearchString#" cfsqltype="CF_SQL_VARCHAR" maxlength="255"> LIMIT 1   
</cfquery> 

<cfif ListOfLocations.recordcount>
	<cflocation url="https://example.com/template.cfm?pm=ms&loc_id=#ListOfLocations.id#" addtoken="no" statusCode="301">
<cfelse>
	<cflocation url="/404error.cfm" addtoken="no">
</cfif>

【讨论】:

    【解决方案3】:

    这就是当今最流行的 MVC 框架的工作方式,通过解析 URL 段。

    您可以使用任何类型的 URL 重写软件吗?

    由于您使用的是 IIS,它有一个内置的重写引擎,您可以在其中简单地将这些类型的请求重写到一个已知文件,从而节省发送 404 回复和解析它并创建更多请求的开销那。

    详情请参阅http://wiki.coldbox.org/wiki/URLMappings.cfm。我们使用 Isapi 重写版本来满足您的要求

    • 接收 www.mysite.com/thisPlace 的请求
    • thisPlace 不是目录
    • 这个地方不是文件
    • 重新发送到 index.cfm 或您选择的位置以进行其他解析
    • Helicon Rewrite 发送一个名为 HTTP_X_REWRITE_URL 的 HTTP 标头,其中包含原始请求的 URL,因此解析它非常容易。

    这一切都发生在一个请求中,因此客户端永远不会被重定向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 2010-11-04
      • 1970-01-01
      相关资源
      最近更新 更多