URL 重写和 301 重定向...重定向到原始 URL
在回答另一个人的问题时,我发现我的全局重定向代码中有一个小“错误”。
我已将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是有效的答案
Here's the answer that works
而不是这个:
试试这个:
Instead of this:
Try this:
使用 IIS 并设置不同的网站。进行永久重定向,保留 url。
Use IIS and setup a defferent website. Do a permanent redirect, preserving the url.
尝试使用Request.RawUrl,例如
Request.RawUrl应该是实际http请求的原始URL。
Try use Request.RawUrl, e.g.
Request.RawUrl should be the original URL from the actual http request.