允许 HTACCESS 重写内的 GET 变量
现在我有一个如下所示的重写规则:
RewriteRule ^product-([a-z^-]+).php /sample.php?m=$1
如果我尝试访问“product-test.php”,它工作正常。
如果我尝试访问“product-test.php?variable=1”,它将不起作用。
如何修改重写规则以允许 GET 变量?
注意:GET 变量应仅允许字母 (Aa-Zz) 和破折号 (-)。
Right now I have a rewrite rule that looks like this:
RewriteRule ^product-([a-z^-]+).php /sample.php?m=$1
If I try going to "product-test.php", it works fine.
If I try going to "product-test.php?variable=1", it will not work.
How can I modify the rewrite rule to allow the GET variable?
NOTE: The GET variable should only allow letters (Aa-Zz) and dashes (-).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信它应该是
“注意转义破折号的反斜杠”。破折号是匹配部分中的运算符,因此必须对其进行转义。
另外,要包含具有重写 url 的 GET 变量而不重写它们,请将查询字符串标识符附加到末尾。
这会将:
www.example.com/product-widget.php?variable=12
重写为
www.example.com/sample.php?m=widget&variable=12
>我相信这就是您想要的。
话虽如此,我不确定转义破折号是否真的有效,因为我刚刚发现在重写的网址中使用破折号存在一个问题,但我似乎无法解决该问题。就像 Apache 1.3.33 重写模块不喜欢重写字符串中的破折号一样。无论如何,这就是它应该如何工作,并且它在我的服务器上工作。
I believe it should read
Note the backslash escaping the dash. The dash is an operator in the matching portion, so it must be escaped.
Also, to include GET variables with a rewritten url without rewriting them, append the query string identifier to the end.
This will rewrite:
www.example.com/product-widget.php?variable=12
to
www.example.com/sample.php?m=widget&variable=12
Which is what I believe you are going for.
That having been said, I'm not sure escaping the dash actually works, as I've just discovered a problem using a dash in my rewritten urls that I can't seem to resolve. It's like the Apache 1.3.33 rewrite module doesn't like the dash in the rewrite string. Anyways, this is how it should work, and it works on my server.