将动态 URL 重写为新的动态 URL
我是 RewriteEngine 的新手,无法找到以下问题的答案。我经营一个电子商务网站,其中的产品 SKU 目录不断变化。我们的 URL 是动态的。问题是,如果我想要将动态变量重定向到不同的动态变量怎么办?
例如,我想要:
http://www.mydomain.com/product.jhtm? id=12345
现在请访问:
www.mydomain.com/product.jhtm?
id=78910 我如何通过 .htaccess 执行此操作?
提前致谢。
I am new to the RewriteEngine and have not been able to find an answer to the following issue. I run an ecommerce site with an ever changing catalog of product skus. Our URLs are dynamic. The question is, what if I want to have a dynamic variable redirect to a different dynamic variable.
For instance, I want:
http://www.mydomain.com/product.jhtm?id=12345
to now go to:
www.mydomain.com/product.jhtm?id=78910
How can I do this through the .htaccess?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要使用 RewriteMap 指令。实际上,您可以用任何您想要的语言编写一个应用程序(只要使其可执行),该应用程序读取 STDIN 上的原始 ID,然后在 STDOUT 上输出新 ID。然后,您可以在 RewriteRules 中引用该应用程序。
在应用程序层实现这种逻辑可能比在 .htaccess 文件中更容易,特别是因为外部重写程序必须连续运行,而不是在每次新重写时调用。
You will need to use the External Rewriting Program feature of the RewriteMap directive. Effectively, you write an application in whatever language you want (as long as you make it executable), that reads in the original ID on STDIN, and then outputs the new one on STDOUT. You can then reference that application in your RewriteRules.
It may be easier to implement this kind of logic at your application layer, rather than in a .htaccess file, especially since the External Rewriting Program has to be running continously, rather than being called for each new rewrite.
您需要使用
RewriteMap
的映射类型为prg
,以便让脚本查找原始 ID 并返回新 ID。不要忘记查询字符串只能在RewriteCond
中分解。You'll need to use
RewriteMap
with a map type ofprg
in order to have a script look up the original ID and return the new one. Don't forget that the query string can only be broken apart inRewriteCond
.