是否可以将 apache2 配置为接受附加在 url 末尾并以“/”字符分隔的查询字符串?
例子... 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当浏览器看到的可见 URL 中没有查询字符串时,您无法在服务器(即 Apache)上做任何事情来欺骗客户端 JavaScript 来查看“查询字符串”。
使用 Apache mod_rewrite,您可以在内部重写请求以将
/foobar
转换为查询字符串 - 但这是服务器内部的。浏览器/JavaScript 看不到这一点。您可以实现外部重定向,并将 URL 从
/mypage.html/foobar
明显转换为/mypage.html?foobar
(或/mypage.html?/ foobar
) - 但我希望这不是您所需要的。但是,您不需要将其转换为 JavaScript 的查询字符串即可读取它......
有效文件名后面以斜杠开头的部分(例如本例中的
/foobar
)称为附加路径名信息(又名“路径信息”)。通常,Apache 在文本/html 文件的默认文件处理程序上拒绝此类请求,因此上述请求通常会导致 404 Not Found 响应。但是,您可以通过在根
.htaccess
文件顶部包含以下指令来允许所有 URL 上的路径信息:Apache 现在将提供
/mypage.html
而不是生成 404。浏览器会看到完整的 URL,即/mypage.html/foobar
并且这可以在location
对象的pathname
属性中供 JavaScript 使用(即window.location .pathname
),然后您可以解析它以提取/foobar
(或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...
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: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 thepathname
property of thelocation
object (ie.window.location.pathname
) which you can then parse to extract/foobar
(orfoobar
).For example:
pathInfo
is then your "query string".