我该如何摆脱? (问号)在 URL 中标识查询字符串的开头?

发布于 2024-12-09 06:44:25 字数 1332 浏览 2 评论 0原文

我正在使用 ColdFusion 9.1.2。

我建立了一个新网站,用于解析域名和斜杠后的查询字符串。剩下的是 MusicianID 和用于帮助 SEO 的字符串。 URL 如下所示:

http://awesomealbums.info/?1085/jim-croce
http://awesomealbums.info/?1077/james-taylor

当我使用 Facebook 共享它时,Facebook 会删除问号并对其进行编码。他们似乎无法解析它,因此将其显示为主页。

这些会引发一个我无法控制的错误:

http://awesomealbums.info/1085/jim-croce
http://awesomealbums.info/1077/james-taylor

我注意到 StackOverlfow 和其他站点能够排除启动查询字符串的问号。我也想做同样的事情。但是,我无法更改任何 IIS 或 CF 管理员设置。我需要编写解决方案的代码。我已经尝试过,但 IIS 告诉我他们找不到该页面。

我希望我的 URL 看起来像这样:

http://awesomealbums.info/1085/jim-croce // same as above but no ?
http://awesomealbums.info/1077/james-taylor // same as above but no ?

这是我现在用来解析 URL 并获取 MusicianID 的代码。

 <cfscript>
QString = CGI.QUERY_STRING;
if (QString eq "") {
    include "Home.cfm";
} else if (QString eq "WhoAmI") {
    include "WhoAmI.cfm";
} else {
    IndexOfSlash = Find("/", QString);
    if (IndexOfSlash eq 5) {
        ThisID = left(QString, 4);
        if (isNumeric(ThisID)) {
            MusicianID = ThisID;
            include "Musician.cfm";
        }
    } else {
        location(url="http://www.awesomealbums.info" addtoken="false");
    }
}

如何更改我的网站,以便删除问号并且网络服务器不会变得奇怪并且我可以解析查询字符串?

I am using ColdFusion 9.1.2.

I set up a new web site that parses the query string after the domain name and slash. What is left is the MusicianID and then a string used to help with SEO. The URL looks like this:

http://awesomealbums.info/?1085/jim-croce
http://awesomealbums.info/?1077/james-taylor

When I share it using Facebook, Facebook removes the question mark and encodes it. They can't seem to parse it so they display it as the home page.

These throw an error that I can't control:

http://awesomealbums.info/1085/jim-croce
http://awesomealbums.info/1077/james-taylor

I notice that StackOverlfow and other sites are able to exclude the question mark that starts a query string. I would like to do the same. I, however, can't change any IIS or CF Administrator settings. I need to code the solution. I've tried, but I get IIS telling me they can't find the page.

I want my URLs to look like this:

http://awesomealbums.info/1085/jim-croce // same as above but no ?
http://awesomealbums.info/1077/james-taylor // same as above but no ?

Here's the code that I am using right now to parse the URL and get the MusicianID.

 <cfscript>
QString = CGI.QUERY_STRING;
if (QString eq "") {
    include "Home.cfm";
} else if (QString eq "WhoAmI") {
    include "WhoAmI.cfm";
} else {
    IndexOfSlash = Find("/", QString);
    if (IndexOfSlash eq 5) {
        ThisID = left(QString, 4);
        if (isNumeric(ThisID)) {
            MusicianID = ThisID;
            include "Musician.cfm";
        }
    } else {
        location(url="http://www.awesomealbums.info" addtoken="false");
    }
}

How can I alter my site so that the question mark can be removed and the web server doesn't get funky and I can parse out the query string?

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

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

发布评论

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

评论(4

回首观望 2024-12-16 06:44:25

您正在寻找的关键字是URL重写。它必须发生在 Web 服务器上,因为您想要处理顶级目录中的所有请求。如果您的 Web 服务器是 Apache httpd,您可以这样做:

RewriteEngine on
RewriteRule ^/(\d{4})/([\w-]+)$ /?$1/$2 [L]

或者

RewriteRule ^/(\d{4})/([\w-]+)$ /Musician.cfm?MusicianID=$1 [L]

The keyword you are looking for is URL rewriting. It has to happen on the web server, since you want to handle all requests in the top-level directory. If your web server is the Apache httpd, you can do it like this:

RewriteEngine on
RewriteRule ^/(\d{4})/([\w-]+)$ /?$1/$2 [L]

or

RewriteRule ^/(\d{4})/([\w-]+)$ /Musician.cfm?MusicianID=$1 [L]
绅刃 2024-12-16 06:44:25

由于您无法修改网络服务器(正如 Roland 正确建议的那样),因此有一种替代方法 - 使用如下所示的 URL:

http://awesomealbums.info/index.cfm/1085/jim-croce

以这种方式构建,网络服务器(IIS)仍然会将控制权传递给您的脚本。然后您可以开始让 CF 接管处理控制。您的 CGI.query_string 将为空,但您的 cgi.path_info 变量将包含 /1085/jim-croce。然后您可以开始解析它并根据需要进行处理。

Since you can't modify the web server (as Roland correctly suggests) there is one alternative - use URLs that look like this:

http://awesomealbums.info/index.cfm/1085/jim-croce

Structured this way, the webserver (IIS) will still pass control to your script. Then you can start having CF take over control of the processing. Your CGI.query_string will be empty, but your cgi.path_info variable will contain /1085/jim-croce. You can then start parsing that and handling it as needed.

不知在何时 2024-12-16 06:44:25

IIS的答案与Roland在他的答案中提到的基本相同。您需要使用 URL 重写,这是完成您想要做的事情的唯一方法。这是因为从技术上讲,您要请求的 URL 并不作为服务器上的真实页面或资源存在,您需要使用 URL 重写来拦截页面请求,使用正则表达式将其映射,然后将其传递给您的应用程序作为您期望的查询字符串(页面参数)。因此,如果您在托管服务器上执行此操作,请联系您的主机并查看他们是否在服务器上设置或安装了用于执行 URL 重写的内容。大多数体面的主人肯定会这样做。

如果您使用的是 IIS7,则可以在以下链接找到有关使用内置 URL 重写的信息:

http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

如果您使用的是旧版本的 IIS,那么你需要要在服务器上安装一个应用程序来执行此操作,因为以前版本的 IIS 没有内置对基于正则表达式的重写的支持,您需要将 URL 正确映射到查询字符串上的正确参数。对于旧版本的 IIS,我使用了 Helicon IsapiRewrite,您可以在以下位置找到它:

http://www.isapirewrite.com/

The answer for IIS is basically the same as Roland mentions in his answer. You need to use URL Rewriting, that is the only way to accomplish what you are looking to do. This is because technically the URL that you want to request, does not exist as a real page or resource on the server, and you need to use URL Rewriting to intercept the page request, map it using regular expressions, then pass it to your application as the query string (page parameters) that you are expecting. So, if you are doing this on a hosted server, contact your host and see if they have something setup or installed on the server for doing URL Rewriting. Most any decent host certainly will.

If you are using IIS7, then info on using the built in URL Rewriting can be found at the link:

http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

If you are using an older version of IIS, then you need to install an application on the server that will do this as previous versions of IIS do not have built in support for regular expression based rewriting that you would need to properly map your URLs to the correct parameters on your query string. For older versions of IIS, I've used Helicon IsapiRewrite which you can find at:

http://www.isapirewrite.com/

烟若柳尘 2024-12-16 06:44:25

摆脱jfeasel的答案,您可以使用ColdCourse:

http://coldcourse.riaforge.org/

riding off of jfeasel's answer, you can use ColdCourse:

http://coldcourse.riaforge.org/

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