开发者

How do I write a 301 redirect taking a portion from the middle of the old url and using it on the end of the new?

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开发者_JAVA百科-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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜