问题描述
我需要生成用户友好的网址。我在IIS上。
I have got a requirement for generating user friendly urls.I am on IIS.
我的动态网址如下,
www.testsite.com/blog/article.cfm?articleid=4432
客户需要网址应如下所示
Client wants the urls should look like
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,
- 用户将点击网址
www.testsite.com/blog/article_title
- 我需要使用网址中的article_title获取文章ID。
- 使用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#
做类似的事情。亚当塔特尔在这里解释说,你可以跳过一些箍:。
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中使用此功能,所有受影响的页面都会在此事件中抛出任何缺失的网址。然后你可以放置
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!