获取 htaccess 以在重定向时将值添加为 GET 变量

发布于 2024-09-16 17:40:13 字数 598 浏览 2 评论 0原文

简而言之,我想要实现的是,

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 技术交流群。

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

发布评论

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

评论(1

┊风居住的梦幻卍 2024-09-23 17:40:13

你这样做的方式很好,但请记住,你也可以使用 QSA 标志。这会自动将现有查询字符串附加到您通过重写生成的查询字符串,自动为您处理连接。

这看起来像下面这样:

RewriteRule ^food(/)?$ show_products.php?cat_id=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:

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