是否可以将 apache2 配置为接受附加在 url 末尾并以“/”字符分隔的查询字符串?

发布于 2025-01-16 00:21:15 字数 207 浏览 0 评论 0原文

例子... https://myisp.com/mypage.html/foobar 我希望能够在 mypage.html 上运行一些 js,它可以读取 foobar ,就好像它是查询参数一样。我了解如何编写必要的 js,但我试图了解 Apache 是否可以配置为提供 html 页面并将最终术语传递给在页面上运行的脚本。

Example...
https://myisp.com/mypage.html/foobar
I would like to be able to have some js running on mypage.html that can read the foobar as if it were a query parameter. I understand how to write the necessary js, but am trying to understand if Apache can be configured to serve up the html page and pass the final term to a script running on the page.

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

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

发布评论

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

评论(1

﹏半生如梦愿梦如真 2025-01-23 00:21:15

当浏览器看到的可见 URL 中没有查询字符串时,您无法在服务器(即 Apache)上做任何事情来欺骗客户端 JavaScript 来查看“查询字符串”。

使用 Apache mod_rewrite,您可以在内部重写请求以将 /foobar 转换为查询字符串 - 但这是服务器内部的。浏览器/JavaScript 看不到这一点。

您可以实现外部重定向,并将 URL 从 /mypage.html/foobar 明显转换为 /mypage.html?foobar (或 /mypage.html?/ foob​​ar) - 但我希望这不是您所需要的。

但是,您不需要将其转换为 JavaScript 的查询字符串即可读取它......

<代码>/mypage.html/foobar

有效文件名后面以斜杠开头的部分(例如本例中的 /foobar)称为附加路径名信息(又名“路径信息”)。通常,Apache 在文本/html 文件的默认文件处理程序上拒绝此类请求,因此上述请求通常会导致 404 Not Found 响应。

但是,您可以通过在根 .htaccess 文件顶部包含以下指令来允许所有 URL 上的路径信息:

AcceptPathInfo On

Apache 现在将提供 /mypage.html 而不是生成 404。浏览器会看到完整的 URL,即/mypage.html/foobar 并且这可以在 location 对象的 pathname 属性中供 JavaScript 使用(即 window.location .pathname),然后您可以解析它以提取 /foobar (或 foobar)。

例如:

// Request "/mypage.html/foobar"
let pathInfo = /\.html\/(.*)/.exec(window.location.pathname)[1];
console.log(pathInfo); // foobar

pathInfo 就是您的“查询字符串”。

There's nothing you can do on the server (ie. Apache) to fool client-side JavaScript in to seeing a "query string" when there is no query string in the visible URL that the browser sees.

Using Apache mod_rewrite you can internally rewrite the request to convert /foobar into a query string - but that's internal to the server. The browser/JavaScript does not see that.

You could implement an external redirect and visibly convert the URL from /mypage.html/foobar to /mypage.html?foobar (or /mypage.html?/foobar) - but I expect that is not what you require.

However, you don't need to convert this to a query string for JavaScript to be able to read it...

/mypage.html/foobar

The part that starts with a slash after a valid filename (eg. /foobar in this example) is called additional pathname information (aka "path-info"). Normally, Apache rejects such requests on the default file handler for text/html files, so the above request would normally result in a 404 Not Found response.

However, you can allow path-info on all URLs by including the following directive at the top of the root .htaccess file:

AcceptPathInfo On

Apache will now serve /mypage.html instead of generating a 404. The browser sees the full URL, ie. /mypage.html/foobar and this is available to JavaScript in the pathname property of the location object (ie. window.location.pathname) which you can then parse to extract /foobar (or foobar).

For example:

// Request "/mypage.html/foobar"
let pathInfo = /\.html\/(.*)/.exec(window.location.pathname)[1];
console.log(pathInfo); // foobar

pathInfo is then your "query string".

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