是否可以使用 ColdFusion 重写 url? [英] Is it possible to rewrite url using ColdFusion?

查看:26
本文介绍了是否可以使用 ColdFusion 重写 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成用户友好的 url.我在 IIS 上.

I have got a requirement for generating user friendly urls.I am on IIS.

我的动态网址看起来像,

My dynamic URLs looks like,

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

客户希望网址看起来像

www.testsite.com/blog/article_title

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

I know this can be easily done using IIS URL rewiter 2.0.

但客户只想使用 ColdFusion 来做到这一点.他给出的基本想法,

But the Client wants to do it using ColdFusion only. Basic idea he given like,

  1. 用户将点击 url www.testsite.com/blog/article_title
  2. 我需要使用 url 中的 article_title 获取文章 ID.
  3. 使用 ID 调用 article.cfm 页面并将输出加载到 cfsavecontent 中,然后将该输出传送到浏览器.

但我认为在应用服务器级别是不可能的.IIS 如何理解我们的用户友好 url.或者我错过了什么重要的东西?是否可以在应用服务器级别使用 ColdFusion 来做到这一点?

But I do not think its possible at application server level. How IIS will understand our user friendly urls . OR am I missing something important? Is it possible to do it using ColdFusion at application server level?

推荐答案

首先,我讨厌建议重新发明轮子.网络服务器做到这一点并且做得很好.

First, I hate to recommend reinventing the wheel. Webservers do this and do this well.

Cold Fusion 可以使用 #cgi.path_info# 做类似的事情.正如 Adam Tuttle 在这里解释的那样,您可以跳过一些障碍:我可以在 IIS 中没有 URL 重写器的情况下拥有友好"的 URL 吗?.

Cold Fusion can do something like this with #cgi.path_info#. You can jump through some hoops as Adam Tuttle explains here: Can I have 'friendly' url's without a URL rewriter in IIS?.

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

Option #2: My Favorite: OnMissingTemplate..

仅适用于 Application.cfc 的用户(我很确定 .cfm 没有对应的 onMissingTemplate).

Only available to users of Application.cfc (I'm pretty sure .cfm has no counterpart to onMissingTemplate).

您可以在 application.cfc 中使用此功能,所有受影响的页面将在此事件中抛出任何丢失"的 url.然后你可以放置

You can use this function within application.cfc and all affected pages will throw any "missing" urls at this event. You can then place

<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>

mt_blog.cfm 可以有这样的内容,如果你的网址就像/blog/How-to-train-your-flea-circus.cfm

mt_blog.cfm can have contents like, if your url is say just like /blog/How-to-train-your-flea-circus.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)

Or if you use a url like /blog/173_How-to-train-your-flea-circus.cfm (where 173 is a post 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.

...

这篇关于是否可以使用 ColdFusion 重写 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆