URL重写和301重定向...重定向到原始URL

时间:2010-02-18 17:53:29

标签: asp.net vb.net http-status-code-301 url-rewriting

在SO上回答另一个人问题时,我发现我的全局重定向代码中存在一个小“错误”。

我已将Global类连接到HttpModule。它的工作是检测“http:/ www。”在URL中并将用户重定向到NON www。版本

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 = If(url.ToString.StartsWith("http://www."), True, False) 'UrlRegex.IsMatch(url.ToString())
    If hasWWW Then
        Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme))
        application.Context.Response.Redirect(newUrl, False)
        application.Context.Response.StatusCode = 301
        application.Context.Response.End()

    End If

End Sub

我遇到的问题是,当它重定向网页http://www.example.com/AboutUs时,目标是让它转到http://example.com/AboutUs(重写的网页),而是转到http://example.com/Default.aspx?Slug=AboutUs (原始页面)。

我尝试通过更改

来做一些黑客攻击
    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", "")) 

    Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme))
    newUrl = newUrl.Replace("Default.aspx?Slug=", "")
    newUrl = newUrl.Replace("Default.aspx", "")
    application.Context.Response.Status = "301 Moved Permanently"
    application.Context.Response.AddHeader("Location", newUrl) 

不是我想要做的事,因为它是一个黑客,但它无论如何都没有用。

非常感谢任何关于此的建议!

4 个答案:

答案 0 :(得分:1)

这是有效的答案

    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 = If(url.ToString.StartsWith(String.Format("{0}://www.", url.Scheme)), True, False)
        Dim forceWWW As Boolean = Boolean.TryParse(ICMS.Site.Settings.GetSettingsValue("ForceWWW"), False)
        'UrlRegex.IsMatch(url.ToString())
        If hasWWW Then
            Dim newUrl As String = UrlRegex.Replace(url.ToString(), String.Format("{0}://", url.Scheme))
            application.Context.Response.Redirect(newUrl.Replace("Default.aspx?Slug=", String.Empty), False)
            application.Context.Response.StatusCode = 301
            application.Context.Response.End()
        End If

    End Sub

答案 1 :(得分:0)

而不是:

application.Context.Response.Status = "301 Moved Permanently"
application.Context.Response.AddHeader("Location", newUrl) 

试试这个:

application.Context.Response.Redirect(newUrl, false)
application.Context.Response.StatusCode = 301
application.Context.Response.End()

答案 2 :(得分:0)

使用IIS并设置其他网站。进行永久重定向,保留网址。

答案 3 :(得分:0)

尝试使用Request.RawUrl,例如

Dim newUrl As [String] = UrlRegex.Replace(Request.RawUrl.ToString(), [String].Format("{0}://", url.Scheme))

Request.RawUrl应该是来自实际http请求的原始URL。