如何编写 301 重定向,从旧 url 的中间提取一部分并在新 url 的末尾使用它?

发布于 2024-08-22 05:52:30 字数 430 浏览 7 评论 0原文

我需要将旧购物车链接重定向到新网站的搜索,

这是一个示例:

旧网址: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 技术交流群。

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

发布评论

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

评论(2

¢好甜 2024-08-29 05:52:30

我假设他使用的是 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 the Detail directive and put it into the query string for the new URL.

安静被遗忘 2024-08-29 05:52:30

以下是我在 VB.NET 中执行此操作的方法

Protected Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    'Force Removal of WWW
    Dim application As HttpApplication = TryCast(sender, HttpApplication)
    Dim url As Uri = application.Context.Request.Url
    Dim hasWWW As Boolean = UrlRegex.IsMatch(url.ToString())
    If hasWWW Then
        Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme))
        application.Context.Response.Status = "301 Moved Permanently"
        application.Context.Response.AddHeader("Location", newUrl.Replace("default.aspx", ""))
    End If

End Sub

下面的示例位于我的 Global 类中,我在其中重定向到非 WWW url。你可以根据自己的喜好修改它,301重定向是一样的。

编辑:上面的答案是在OP澄清他们正在使用的技术之前给出的

Here's how I do it in VB.NET

Protected Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    'Force Removal of WWW
    Dim application As HttpApplication = TryCast(sender, HttpApplication)
    Dim url As Uri = application.Context.Request.Url
    Dim hasWWW As Boolean = UrlRegex.IsMatch(url.ToString())
    If hasWWW Then
        Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme))
        application.Context.Response.Status = "301 Moved Permanently"
        application.Context.Response.AddHeader("Location", newUrl.Replace("default.aspx", ""))
    End If

End Sub

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文