使用 Coldfusion,您如何处理动态生成的 URL?

发布于 2024-08-14 18:41:44 字数 1139 浏览 3 评论 0原文

(更新:我将此问题转换为社区 wiki,因为答案似乎比我想象的更主观。根据需要有多个答案。)

如果我有一个仅包含 application.cfc 和 index.cfm 的文件夹,那么什么是处理动态生成的 URL 的快速、可靠的方法吗?即没有相应物理 .cfm 文件的 URL。

此示例 url 生成 404,但它应该在数据库中查找页面并通过 index.cfm 返回它:

http://www.myserver.com/cfdemo/mynewpage.cfm

我应该使用 onMissingTemplate() 在 application.cfc 中处理丢失的文件?由于这个方法不处理onRequestStart()、onRequest()和onRequestEnd(),我想知道是否应该避免它。

或者,我可以设置 ISAPIRewrite 规则,因为我使用的是 IIS(或 Apache 上的 mod_rewrite)。

# IF the request is not /index.cfm, doesn't exist and ends in cfm or html,
# rewrite it. Pass the requested filename $1.$2 as the 1st param: cgi.page
# append the remaining url params $4 ($3 is the ?)
RewriteCond %{SCRIPT_NAME} ^(?!/index.cfm)(.*)$
RewriteCond %{REQUEST_FILENAME}     !-f
RewriteCond %{REQUEST_FILENAME}     !-d 
RewriteRule ^\/(.*)\.(cfm|html)(\??)(.*)$   /index.cfm?page=$1.$2&$4 [I,L]

这些方法是否合适,或者我是否缺少实现此目标的更好方法?看来 Coldfusion 应该将此类功能内置到 application.cfc 中。也许我只是想念它。

(Update: I converted this question to a community wiki as the answer appears more subjective than I thought it would. There are multiple answers depending on one's needs.)

If I have a folder that only includes application.cfc and index.cfm, what is a fast, reliable method to handle dynamically generated URLs? i.e. URLs that do not have a corresponding physical .cfm file.

This example url generates a 404, but it should lookup a page in a db and return it via index.cfm:

http://www.myserver.com/cfdemo/mynewpage.cfm

Should I use onMissingTemplate() in the application.cfc to handle the missing file? Since this method doesn't process onRequestStart(), onRequest() and onRequestEnd(), I wonder if it should be avoided.

Alternately, I could setup an ISAPIRewrite rule since I'm using IIS (or mod_rewrite on Apache)

# IF the request is not /index.cfm, doesn't exist and ends in cfm or html,
# rewrite it. Pass the requested filename $1.$2 as the 1st param: cgi.page
# append the remaining url params $4 ($3 is the ?)
RewriteCond %{SCRIPT_NAME} ^(?!/index.cfm)(.*)$
RewriteCond %{REQUEST_FILENAME}     !-f
RewriteCond %{REQUEST_FILENAME}     !-d 
RewriteRule ^\/(.*)\.(cfm|html)(\??)(.*)$   /index.cfm?page=$1.$2&$4 [I,L]

Are these methods appropriate, or am I missing a better way of accomplishing this goal? It seems that Coldfusion should have this type of feature built into the application.cfc. Maybe I'm just missing it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月蹉跎了容颜 2024-08-21 18:41:45

Web 服务器级别的 url 重写没有任何问题。我会投票赞成。

nothing wrong with url rewrite on web server level. I'd vote for that.

久随 2024-08-21 18:41:45

因为 CF 默认情况下只处理 cfm/cfc 请求,所以您可以在 Application.cfc 的开头执行如下操作:

<cfif Right(cgi.SCRIPT_NAME, 9) NEQ "index.cfm">
    <!--- analyze the SCRIPT_NAME and start processing --->
</cfif>

对于其他文件类型,使用 Web 服务器配置是我能看到的唯一方法。但您可以尝试使用自定义 404 处理程序,而不是创建重写规则。至少在使用 IIS 时,您将能够获取 cgi.QUERY_STRING 中的上下文,如果设置虚拟页面,请说 404.cfm (它不需要存在)并进行以下检查 < em>before 前面的示例:

<!--- trap 404 requests triggered by IIS --->
<cfif right(cgi.SCRIPT_NAME, 7) EQ "404.cfm">
    <cflog file="mylogfile" text="404 error triggered by IIS. Context: #cgi.QUERY_STRING#">
</cfif>

对于 Apache,可以使用以下处理程序,但我不确定在这种情况下是否可以提取上下文:

ErrorDocument 404 /404.cfm

Because CF by default handles only cfm/cfc requests, you can do in the beginning of Application.cfc something like this:

<cfif Right(cgi.SCRIPT_NAME, 9) NEQ "index.cfm">
    <!--- analyze the SCRIPT_NAME and start processing --->
</cfif>

For other filetypes using web-server configuration is the only way I can see. But instead of creating rewriting rules you can try to use custom 404 handlers. At least when using IIS you'll be able to get the context in cgi.QUERY_STRING, if set up the dummy page, say 404.cfm (it does not need to exist) and putting following check before previous example:

<!--- trap 404 requests triggered by IIS --->
<cfif right(cgi.SCRIPT_NAME, 7) EQ "404.cfm">
    <cflog file="mylogfile" text="404 error triggered by IIS. Context: #cgi.QUERY_STRING#">
</cfif>

For Apache it is possible to use following handler, but I'm not sure if you can extract the context in this case:

ErrorDocument 404 /404.cfm
三月梨花 2024-08-21 18:41:45

如果您对 SES URL 执行此操作,我会提供两条建议。

首先,随着时间的推移,它们的重要性越来越小。例如,Google 认识到 URL 需要包含查询数据。

其次:CF 可以原生处理格式为 hostname/file.cfm/param1/param2 的 SES URL。例如,Ray Camden 的 BlogCFC 就是这样工作的。在 CF8 中默认开启,但在 CF7 中需要启用。我手头没有太多这方面的信息,但用谷歌(或必应,或其他什么)应该很容易。

If you are doing this for SES URLs, I'd offer two pieces of advice.

The first is that they matter less and less as time goes on. Google, for example, recognizes that URLs need to include query data.

Second: CF can natively handle SES URLs in the form hostname/file.cfm/param1/param2. Ray Camden's BlogCFC, for example, works that way. It is on by default in CF8, but needs to be enabled in CF7. I don't have a lot of information handy on this, but it should be easy to Google (or Bing, or whatever).

蹲在坟头点根烟 2024-08-21 18:41:45

如果您允许的话,我会尝试将以下 URL 转换为:

http://www.myserver.com/cfdemo/mynewpage.cfm

http://www.myserver.com/cfdemo/mynewpage OR
http://www.myserver.com/index.cfm/cfdemo/mynewpage

这样您就不会丢失 onRequest 方法。第一个只能在 Web 服务器级别完成,因此在 Apache 或 IIS 中。第二个可以仅在 ColdFusion 中完成。请参阅:http://www.cfcdeveloper.com /index.cfm/2007/4/7/Coldfusion-SES-URL

否则,如果末尾必须有 .cfm,则可以使用 Apache 或 IIS 中的 URL 重写包将其删除,然后将请求转发到 cfm 页面或执行您想要的操作使用onMissingTemplate 进行操作。我会尝试选择一个不会丢失 onRequest 方法的解决方案,但由您决定。

If you can allow it, I'd try to convert URLs like:

http://www.myserver.com/cfdemo/mynewpage.cfm

to:

http://www.myserver.com/cfdemo/mynewpage OR
http://www.myserver.com/index.cfm/cfdemo/mynewpage

so that you don't lose the onRequest methods. The first one can be done only at the webserver level, so in Apache or IIS. The second one can be done in just ColdFusion. See this: http://www.cfcdeveloper.com/index.cfm/2007/4/7/Coldfusion-SES-URL.

Otherwise, if you must have the .cfm at the end, you can use a URL rewrite package in Apache or IIS to strip it out and then forward the request to a cfm page or do what you're doing with onMissingTemplate. I'd try to opt for a solution that doesn't involve losing the onRequest methods, but up to you.

飘落散花 2024-08-21 18:41:45

我肯定会进行 URL 重写。它不仅是一种更可预测且通用的方法,而且可以减少 CF 服务器的大量字符串解析负载。此外,它会导致 CF 处理对真实文件的请求,从而使您受益于 onapplicationstart、onrequeststart 和其他事件。

顺便说一句,我个人总是发现像 /index.cfm/foo/bar/ 这样的 URL 看起来不专业而且很黑客。此外,不以文件扩展名或尾部斜杠结尾的 URL(例如 /foo/bar)在技术上是不正确的(至少按照老式静态站点约定),也应该避免。我也很好奇 Ben Doom 从哪里得到他的断言:“首先,随着时间的推移,它们的重要性越来越小。例如,Google 认识到 URL 需要包含查询数据。”根据我的经验,我实际上发现事实恰恰相反。

I'd definitely go for URL rewriting. Not only will it be a more predictable, yet generalized approach, but it reduces a significant amount of string parsing load from the CF server. Further, it results in CF handling a request to a real file thereby getting you the benefit of onapplicationstart, onrequeststart, and other events.

As an aside, I've personally always found URLs like /index.cfm/foo/bar/ to look unpro and hackish. Additionally, URLs (like /foo/bar) that don't end in either a file extension or trailing slash are technically incorrect (per old-school static site conventions at the very least) and ought to probably be avoided as well. I'd also be curious where Ben Doom gets his assertion that "The first is that they matter less and less as time goes on. Google, for example, recognizes that URLs need to include query data." In my experience I've actually found the exact opposite to be true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文