如何编写 301 重定向,从旧 url 的中间提取一部分并在新 url 的末尾使用它?
我需要将旧购物车链接重定向到新网站的搜索,
这是一个示例:
旧网址:http://www.example.com/servlet/the-1736/Festo-Line-Acuator-DNC-dsh-100 -dsh-150-dsh-PPVA-dsh-Q/Detail
新的应该是什么样子:www.example.com/cart/index.php?dispatch=search.results&q=Festo-Line -Acuator-DNC-dsh-100-dsh-150-dsh-PPVA-dsh-Q
因此,在我的旧 URL 上,它们始终源自子目录“servlet”,下一部分是变量类别(在此案例“the-1736”),然后是我想在新的 url 字符串中使用的部分,然后下一个斜杠之后的所有内容都应该被忽略。
I need to redirect my old cart links to the new site's search
Here's an example:
old url:http://www.example.com/servlet/the-1736/Festo-Line-Acuator-DNC-dsh-100-dsh-150-dsh-PPVA-dsh-Q/Detail
what new should look like: www.example.com/cart/index.php?dispatch=search.results&q=Festo-Line-Acuator-DNC-dsh-100-dsh-150-dsh-PPVA-dsh-Q
So on my old URL's they would always originate from the subdirectory "servlet", the next piece is a variable category (in this case "the-1736"), then the piece I want to use in the new url string, then everything after that next slash should be ignored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设他使用的是 PHP,因为这是第二个 URL 上的扩展名。由于“servlet”部分,我还假设旧 URL 有一个 J2EE 后端。
我不是 url-rewrite 方面的专家,但看来你应该能够做到这一点。我想这取决于实际的后端,假设基于 Apache 的 PHP。这还取决于更换是否像您的问题中看起来那么简单。
另外,路径
servlet/the-1736
是静态的吗?您也不是 PHP 专家,但您应该能够使用文件路径操纵器获取静态文本之后、Detail
指令之前的路径,并将其放入新 URL 的查询字符串中。I am assuming he is using PHP, as that is the extension on the second URL. I am also assuming a J2EE back-end for the old URL because of the "servlet" part.
I am not an expert in url-rewrite, but it seems that you should be able to do that. I guess it depends on the actual back-end, assuming Apache based PHP. It also depends on if the replacement is as simple as it seems from your question.
Also, is path
servlet/the-1736
static? Not a PHP expert either, but you should be able to use a file path manipulator to take the path after the static text, and before theDetail
directive and put it into the query string for the new URL.以下是我在 VB.NET 中执行此操作的方法
下面的示例位于我的 Global 类中,我在其中重定向到非 WWW url。你可以根据自己的喜好修改它,301重定向是一样的。
编辑:上面的答案是在OP澄清他们正在使用的技术之前给出的
Here's how I do it in VB.NET
The example below is in my Global class where I redirect to a non WWW url. You can modify it to your liking, the 301 redirect is the same.
EDIT: Answer above was given before the OP clarified the technology they were using