为什么此 mod_rewrite 表达式不能正确附加现有查询字符串?

发布于 2024-08-22 08:47:42 字数 675 浏览 4 评论 0原文

我是 mod_rewrite 方面的真正新手,所以请不要笑。我正在使用这个工具进行测试: http://civilolydnad.se/projects/rewriterule/

基本上我想重写

http://hostname/spanish/whatever/

http://hostname/whatever/?lang=es

这是我能想到的最好的:

RewriteRule ^spanish/(.*)$ $1?lang=es [QSA,L,NC]

这似乎可以工作除非当请求的页面已经有参数时。在这种情况下,

http://hostname/spanish/gallery/photos/?page=2

会被重写为“

http://hostname/spanish/gallery/photos/?page=2?lang=es

有人能帮助这个困惑的新手吗?”非常感谢。

I'm a real newbie regarding mod_rewrite so please don't laugh. I am using this tool for testing: http://civilolydnad.se/projects/rewriterule/

Basically I want to rewrite

http://hostname/spanish/whatever/

into

http://hostname/whatever/?lang=es

Here's the best I could come up with:

RewriteRule ^spanish/(.*)$ $1?lang=es [QSA,L,NC]

which seems to work except when the requested page already has parameters. In that case

http://hostname/spanish/gallery/photos/?page=2

gets rewritten as

http://hostname/spanish/gallery/photos/?page=2?lang=es

Can anyone help this confused newbie? Thank you very much.

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

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

发布评论

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

评论(2

你的背包 2024-08-29 08:47:42

基本上,该工具具有误导性,因为在真实服务器环境中使用时,该规则被证明是正确的(如所写)。感谢大家。

Basically, the tool is misleading, because the rule was proven to be correct, as written, when used on a real server environment. Thanks to all.

像这样的事情应该有帮助。

# Remove query strings from URL
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://yourdomain.com%{REQUEST_URI}? [R=301,L]

Something like this should help.

# Remove query strings from URL
RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://yourdomain.com%{REQUEST_URI}? [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文