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

发布于 2024-08-22 10:37:51 字数 1741 浏览 6 评论 0原文

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

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

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: 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) 

因为它是黑客攻击,但无论如何它都不起作用。

任何对此的建议将非常感激!

In answering another persons question here on SO, I discovered that there is a small "bug" in my global redirect code.

I have wired up a Global class to an HttpModule. It's job is to detect "http:/www." in the URL and redirect the user to the NON www. version

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

The problem I'm having is that when it redirect a page http://www.example.com/AboutUs, the goal is to have it go to http://example.com/AboutUs (the rewritten page) but instead it's going to http://example.com/Default.aspx?Slug=AboutUs (the original page).

I tried doing a bit of a hack by changing

    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", "")) 

to

    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) 

not something I want to do anyways since it's a hack, but it didn't work anyways.

Any advice on this would be very much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

放肆 2024-08-29 10:37:51

这是有效的答案

    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

Here's the answer that works

    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
Hello爱情风 2024-08-29 10:37:51

而不是这个:

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()

Instead of this:

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

Try this:

application.Context.Response.Redirect(newUrl, false)
application.Context.Response.StatusCode = 301
application.Context.Response.End()
赠佳期 2024-08-29 10:37:51

使用 IIS 并设置不同的网站。进行永久重定向,保留 url。

Use IIS and setup a defferent website. Do a permanent redirect, preserving the url.

旧人九事 2024-08-29 10:37:51

尝试使用Request.RawUrl,例如

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

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

Try use Request.RawUrl, e.g.

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

Request.RawUrl should be the original URL from the actual http request.

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