GET 变量显示在 .htaccess 上的 URL 中重写不以 / 结尾的 URL

发布于 2024-09-15 12:47:01 字数 626 浏览 1 评论 0原文

如果原始请求不是以正斜杠 (/) 终止,我的重写会出现暴露 GET 变量的问题。

这是我的规则:

# All requests are routed to PHP
RewriteRule ^(.*)$ framework/index.php?framework=$1&%{QUERY_STRING} [L]

如果我访问:

http://www.domain.com/folder/

一切效果很好。如果我访问:

http://www.domain.com/folder

我会被重定向到:

http://www.domain.com/folder/?framework=folder

给出了什么?

I'm having an issue with my rewrites exposing GET variables if the originating request isn't terminated with a forward slash (/).

Here's my rule:

# All requests are routed to PHP
RewriteRule ^(.*)$ framework/index.php?framework=$1&%{QUERY_STRING} [L]

If I visit:

http://www.domain.com/folder/

everything works great. If I visit:

http://www.domain.com/folder

I get redirected to:

http://www.domain.com/folder/?framework=folder

What gives?

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

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

发布评论

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

评论(1

冷清清 2024-09-22 12:47:01

听起来您的网络根目录中存在 /folder 。由于您尚未向 Apache 发送以斜杠结尾的 URL,并且请求的资源是一个文件夹,因此 DirectorySlash 指令强制重定向到更正的 URL。

不幸的是,mod_rewrite 在此重定向发生之前就已经满足了您的请求,虽然它不会更改用于生成重定向的 URI,但它对查询字符串所做的更改不会分开以 mod_dir 知道不包含它们的方式。因此,当重定向发送回浏览器时,它包含由 RewriteRule 生成的查询字符串。

对此的一个潜在解决方案(除了关闭 DirectorySlash 之外,由于文档中列出的原因,不建议这样做)是执行 mod_dir 的工作,作为你的规则集:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTPS}s ^on(s)|off
RewriteRule [^/]$ http%1://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

RewriteRule ^(.*)$ framework/index.php?framework=$1 [QSA,L]

It sounds like /folder exists in your web root. Since you haven't sent Apache a slash-terminated URL, and the requested resource is a folder, the DirectorySlash directive forces a redirect to the corrected URL.

Unfortunately, mod_rewrite has a go at your request before this redirect occurs, and while it doesn't change the URI that is used in generating the redirect, the changes that it makes to the query string are not separated in a way that mod_dir knows not to include them. Therefore, when the redirect is sent back to the browser, it includes the query string generated by your RewriteRule.

A potential solution to this (other than turning off DirectorySlash, which is not recommended for the reasons listed in the documentation) is to perform mod_dir's work for it as part of your rule set:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTPS}s ^on(s)|off
RewriteRule [^/]$ http%1://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]

RewriteRule ^(.*)$ framework/index.php?framework=$1 [QSA,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文