如何在 ColdFusion 中查看客户端 URL?

发布于 2024-07-13 21:33:06 字数 822 浏览 7 评论 0原文

假设,在 ColdFusion 站点上,用户已导航到 http://www.example.com/sub1/

服务器端代码通常用于告诉用户所在的 URL,如下所示: http://#cgi.server_name##cgi.script_name#?#cgi .query_string#

但是,“cgi.script_name”自动包含该文件夹的默认 cfm 文件 - 例如,该代码在解析和展开时将向我们显示“http://www.example.com/sub1/index.cfm"

那么,用户是否正在访问 sub1/index.cfm或 sub1/,“cgi.script_name”变量将包含“index.cfm”。

问题是,如何确定用户实际访问了哪个 URL? 这个问题主要是为了 SEO 目的 - 通常比 301 重定向“/index.cfm”到“/”更可取,以确保任何内容都只有一个 URL - 因为这主要是为了蜘蛛的利益,所以 JavaScript 不是在这种情况下不是一个合适的解决方案。 另外,假设无法访问 isapi_rewrite 或 mod_rewrite - 问题是如何在 ColdFusion 中实现这一目标。

Let's say, on a ColdFusion site, that the user has navigated to
http://www.example.com/sub1/

The server-side code typically used to tell you what URL the user is at, looks like:
http://#cgi.server_name##cgi.script_name#?#cgi.query_string#

however, "cgi.script_name" automatically includes the default cfm file for that folder- eg, that code, when parsed and expanded, is going to show us "http://www.example.com/sub1/index.cfm"

So, whether the user is visiting sub1/index.cfm or sub1/, the "cgi.script_name" var is going to include that "index.cfm".

The question is, how does one figure out which URL the user actually visited? This question is mostly for SEO-purposes- It's often preferable to 301 redirect "/index.cfm" to "/" to make sure there's only one URL for any piece of content- Since this is mostly for the benefit of spiders, javascript isn't an appropriate solution in this case. Also, assume one does not have access to isapi_rewrite or mod_rewrite- The question is how to achieve this within ColdFusion, specifically.

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

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

发布评论

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

评论(5

蓝天 2024-07-20 21:33:06

我想这是不可能的。

如果客户端请求“GET /”,Web 服务器将在调用 ColdFusion 之前将其转换为“GET /{whatever-default-file-exists-fist}”。 (这对于 Web 服务器知道必须首先调用 ColdFusion 是必要的!)

从 ColdFusion(或任何应用程序服务器)的角度来看,客户端请求“GET /index.cfm”,这就是您在 <代码>#CGI#。

正如您自己指出的,可以通过使用 URL 重写工具来进行区分。 既然你明确排除了这条路径,我只能说你在这里运气不好。

I suppose this won't be possible.

If the client requests "GET /", it will be translated by the web server to "GET /{whatever-default-file-exists-fist}" before ColdFusion even gets invoked. (This is necessary for the web server to know that ColdFusion has to be invoked in the first place!)

From ColdFusion's (or any application server's) perspective, the client requested "GET /index.cfm", and that's what you see in #CGI#.

As you've pointed out yourself, it would be possible to make a distinction by using a URL-rewriting tool. Since you specifically excluded that path, I can only say that you're out of luck here.

被翻牌 2024-07-20 21:33:06

不确定仅使用 CF 是否可行,但您可以使用网络服务器的 URL 重写来实现这一点 - 当然,如果您正在使用它们

对于 Apache 来说,它可以是这样的。 比如说,我们使用以下 mod_rewrite 规则:

重写规则^页/([0-9]+)/?$
index.cfm?page=$1&noindex=yes [L]

现在当我们尝试访问 URL http://website.com/page/10/ CGI 显示:

QUERY_STRING 页=10&noindex=是

明白这个想法了吗? 认为使用 IIS 时也可能发生同样的事情。

希望这可以帮助。

Not sure that it is possible using CF only, but you can make the trick using webserver's URL rewriting -- if you're using them, of course.

For Apache it can look this way. Say, we're using following mod_rewrite rule:

RewriteRule ^page/([0-9]+)/?$
index.cfm?page=$1&noindex=yes [L]

Now when we're trying to access URL http://website.com/page/10/ CGI shows:

QUERY_STRING page=10&noindex=yes

See the idea? Think same thing is possible when using IIS.

Hope this helps.

捎一片雪花 2024-07-20 21:33:06

我认为这在CF中是不可能的。 根据我的理解,网络服务器(Apache、IIS 等)确定要显示的默认页面,并向 CF 请求它。 因此,CF并不知道实际调用的页面是什么。

Sergii 是对的,您可以使用 URL 重写来做到这一点。 如果您无法使用此功能,则可以利用默认页面列表中特定页面具有优先级的事实。

我们假设default.htm 是默认页面列表中的第一页。 编写一个自动转发到index.cfm(或其他)的通用default.htm。 如果您可以调整默认值列表,则可以让 CF 执行 301 重定向。 如果没有,您可以在 HTML 文件中执行元刷新、JS 重定向或类似操作。

I do not think this is possible in CF. From my understanding, the webserver (Apache, IIS, etc) determines what default page to show, and requests it from CF. Therefore, CF does not know what the actual called page is.

Sergii is right that you could use URL rewrting to do this. If that is not available to you, you could use the fact that a specific page is given precedence in the list of default pages.

Let's assume that default.htm is the first page in the list of default pages. Write a generic default.htm that automatically forwards to index.cfm (or whatever). If you can adjust the list of defaults, you can have CF do a 301 redirect. If not, you can do a meta-refresh, or JS redirect, or somesuch in an HTML file.

〗斷ホ乔殘χμё〖 2024-07-20 21:33:06

我认为这是可能的。

使用 GetHttpRequestData 您将可以访问所有 HTTP 标头。

然后其中的 GET 标头应该告诉您浏览器正在请求什么文件。

尝试

<cfdump var="#GetHttpRequestData()#"> 

准确查看您可以使用哪些内容。

注意 - 我没有 Coldfusion 来验证这一点。

编辑:经过更多研究,GetHttpRequestData 似乎不包含 GET 标头。 所以这个方法可能行不通。

不过,我确信有一种方法 - 尝试转储 CGI 范围,看看你有什么。

I think this is possible.

Using GetHttpRequestData you will have access to all the HTTP headers.

Then the GET header in that should tell you what file the browser is requesting.

Try

<cfdump var="#GetHttpRequestData()#"> 

to see exactly what you have available to use.

Note - I don't have Coldfusion to hand to verify this.

Edit: Having done some more research it appears that GetHttpRequestData doesn't include the GET header. So this method probably won't work.

I am sure there is a way however - try dumping the CGI scope and see what you have.

要走干脆点 2024-07-20 21:33:06

如果您能够安装 ISAPI_rewrite(假设您在 IIS 上)- http://www.helicontech.com /isapi_rewrite/

它将在 GetHttpRequestData() 结果结构中插入一个变量 x-rewrite-url,该结构将具有 / 或 /index.cfm,具体取决于访问的 URL。

马丁

If you are able to install ISAPI_rewrite (Assuming you're on IIS) - http://www.helicontech.com/isapi_rewrite/

It will insert a variable x-rewrite-url into the GetHttpRequestData() result structure which will either have / or /index.cfm depending on which URL was visited.

Martin

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