是否可以使用ColdFusion重写URL?

时间:2014-12-17 05:55:37

标签: url iis coldfusion url-rewriting coldfusion-10

我需要生成用户友好的网址。我在IIS上。

我的动态网址如下,

www.testsite.com/blog/article.cfm?articleid=4432

客户希望网址看起来像

www.testsite.com/blog/article_title

我知道这可以使用IIS URL rewiter 2.0轻松完成。

但客户只想使用ColdFusion。他给出的基本想法,

  1. 用户将点击网址www.testsite.com/blog/article_title
  2. 我需要使用网址中的article_title来获取文章ID。
  3. 使用ID调用article.cfm页面并将输出加载到cfsavecontent,然后将该输出传递给浏览器。
  4. 但我不认为它在应用程序服务器级别是可能的。 IIS将如何理解我们用户友好的URL。或者我错过了一些重要的事情?是否可以在应用程序服务器级别使用ColdFusion进行此操作?

3 个答案:

答案 0 :(得分:3)

首先,我讨厌建议重新发明轮子。 Web服务器这样做并做得很好。

Cold Fusion可以使用#cgi.path_info#执行此类操作。亚当塔特尔在这里解释说,你可以跳过一些箍:Can I have 'friendly' url's without a URL rewriter in IIS?


选项#2:我最喜欢的:OnMissingTemplate ..

仅适用于Application.cfc的用户(我非常确定.cfm与onMissingTemplate没有对应关系。)

您可以在application.cfc中使用此功能,所有受影响的页面都会抛出任何"缺失的"这个活动的网址。然后你可以放置

<cffunction name="onMissingTemplate">
        <cfargument name="targetPage" type="string" required=true/>
        <!--- Use a try block to catch errors. --->
        <cftry>
                <cfset local.pagename = listlast(cgi.script_name,"/")>
                <cfswitch expression="#listfirst(cgi.script_name,"/")#">
                    <cfcase value="blog">
                        <cfinclude template="mt_blog.cfm">
                        <cfreturn true />
                    </cfcase>
                </cfswitch>
                <cfreturn false />
                <!--- If no match, return false to pass back to default handler. --->
                <cfcatch>
                      <!--- Do some error logging here --->
                      <cfreturn false />
                </cfcatch>
        </cftry>
</cffunction>
如果您的网址与/blog/How-to-train-your-flea-circus.cfm

一样,

mt_blog.cfm可以包含内容

<!--- get everything after the slash and before the dot --->
<cfset pagename = listfirst(listlast(cgi.script_name,"/"),".")>

<!--- you may probably cache queries blog posts --->
<cfquery name="getblogpost">
  select bBody,bTitle,bID
    from Blog
   where urlname = <cfqueryparam cfsqltype="cf_sql_varchar" value="#pagename#">
</cfquery>

<!--- This assumes you will have a field, ex: urlname, that has a url-friendly format to match
      to. The trouble is that titles are generically, in most blogs, changing every special char
      to - or _, so it's difficult to change them back for this sort of comparison, so an add'l
      db field is probably best. It also makes it a little easier to make sure no two blogs have
      identical (after url-safe-conversion) titles. --->

...

或者如果你使用像/blog/173_How-to-train-your-flea-circus.cfm这样的网址(其中173是帖子ID)

<!--- get everything after the slash and before the dot --->
<cfset pageID = listfirst(listlast(cgi.script_name,"/"),"_")>

<!--- you may probably cache queries blog posts --->
<cfquery name="getblogpost">
  select bBody,bTitle,bID
    from Blog
   where bID = <cfqueryparam cfsqltype="cf_sql_integer" value="#pageID#">
</cfquery.

...

答案 1 :(得分:2)

我不建议使用丢失的文件处理程序(或CF的onMissingTemplate)。否则,IIS将返回404状态代码,搜索引擎不会将您的页面编入索引。

您需要做的是确定要使用的唯一前缀模式,并创建web.config重写规则。示例:我有时会在产品详细信息页面上使用“/ detail _”+ id。

如果您不想,则无需保留物理“/ blog”子目录。将以下重写规则添加到Web根目录中的web.config文件,以接受URL中/blog/之后的任何内容,并将其解释为/?blogtitle=[everythingAfterBlog]。 (如果您想继续支持/blog/article.cfm链接,我已添加了一个附加条款。)

<rules>
    <rule name="Blog" patternSyntax="ECMAScript" stopProcessing="true">
        <match url="blog/(.*)$" ignoreCase="true" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{SCRIPT_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{PATH_INFO}" pattern="^.*(blog/article.cfm).*$" negate="true" />
        </conditions>
        <action type="Rewrite" url="/?blogtitle={R:1}" appendQueryString="true" />
    </rule>
</rules>

我建议对新的SEO友好网址使用“301 Redirect”。我还建议在单词片段之间使用破折号( - )并确保字符大小写是一致的(即小写),否则您可能因“重复内容”而受到惩罚。

答案 2 :(得分:1)

要添加到cfqueryparam建议的内容,Using ColdFusion to Handle 404 errors上的这篇文章将介绍如何使用CFM脚本替换 Web服务器的 404处理程序 - 为您提供完全重写功能。它适用于较旧版本的IIS,但您应该能够在正在使用的IIS版本中找到正确的设置。

正如亚当和其他人所说的那样(并且在帖子中也有同样的观点)如果你能避免它,那么你不应该这样做。在HTTP级别工作的Web服务器可以更有效地完成此任务。当您依赖CF来执行此操作时,您有意捕获所引发的错误以获得所需的行为。这是昂贵且不必要的。通常,大多数客户或利益相关者的问题是对URL重写等技术的简单缺乏理解或熟悉。看看你是否可以弯曲它们。祝好运! :)