获取 htaccess 以在重定向时将值添加为 GET 变量
简而言之,我想要实现的是,
www.example.com/show-products/food 应该重定向到 www.example.com/show_products.php?cat_id=1
或 www.example.com/show-products/clothes 应重定向到 www.example.com/show_products.php?cat_id=8
但是,我也希望 htaccess 将 GET 变量从 URL 发送到脚本。 因此 www.example.com/show-products/food?sort=price&sort_order=asc 应该重定向到 www.example.com/show_products.php?cat_id=1?sort=price&sort_order=asc&cat_id=1
我知道我提出问题的方式不是最好的,但希望您能理解它。
谢谢。
我找到了答案,比我想象的要容易,条件是:
RewriteRule ^food(/)?$ show_products.php?cat_id=1&%{QUERY_STRING}
Simply put, what I want to achieve is this,
www.example.com/show-products/food should be redirected to
www.example.com/show_products.php?cat_id=1
or www.example.com/show-products/clothes should be redirected to
www.example.com/show_products.php?cat_id=8
But, I want htaccess to send the GET variables from the URL to the script as well.
So www.example.com/show-products/food?sort=price&sort_order=asc should be redirected to
www.example.com/show_products.php?cat_id=1?sort=price&sort_order=asc&cat_id=1
I know the way I formulated the question is not the best but hopefully you can make sense of it.
Thanks.
I found the answer, it's easier than I expected, the condition is:
RewriteRule ^food(/)?$ show_products.php?cat_id=1&%{QUERY_STRING}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你这样做的方式很好,但请记住,你也可以使用
QSA
标志。这会自动将现有查询字符串附加到您通过重写生成的查询字符串,自动为您处理连接。这看起来像下面这样:
The way that you've done it is fine, but keep in mind that you can also use the
QSA
flag. This automatically appends the existing query string to the query string which you generate with your rewrite, handling the concatenation for you automatically.This would look like the following: