ASP .NET 301 重定向给某些用户带来问题
这是我用来从 www 重定向的代码。到非www。我网站的版本
void Application_BeginRequest(object sender, EventArgs e)
{
string authority = Request.Url.Authority;
if (authority.StartsWith("www."))
{
authority = authority.Remove(0, 4);
string newUrl = "http://" + authority + Request.Url.PathAndQuery;
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", newUrl);
Response.End();
}
}
这是我用来验证上面的代码是否按预期工作的工具:
虽然几乎所有用户的一切都按预期工作,但其中一些人抱怨他们无法访问该网站。我使用 TeamViewer 登录他们的计算机,确实出现了问题。当他们尝试访问该网站时,FF 和 IE 会给出错误:您想要的网站似乎不存在。
应该是什么问题呢?
This is the code I use to redirect from www. to non www. version of my site
void Application_BeginRequest(object sender, EventArgs e)
{
string authority = Request.Url.Authority;
if (authority.StartsWith("www."))
{
authority = authority.Remove(0, 4);
string newUrl = "http://" + authority + Request.Url.PathAndQuery;
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", newUrl);
Response.End();
}
}
this is the tool I used to validate if the code above work as expected:
While everything works as expected for almost all users, some of them has complained they can't access the site. I logged in to their computer using TeamViewer and indeed, there was a problem. When they try to acces the site, FF and IE gives an error: it looks like the site you wanted isn't there.
What should be the problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
.PathAndQuery
,因此请查看以下答案:重定向后超链接在 Firefox 中停止工作?因此,尝试使用
Request.Url.LocalPath
属性。You're using
.PathAndQuery
, so take a look into this answer: Hyperlinks stop working in firefox after redirect?So, try to use
Request.Url.LocalPath
property.