我有一个网站,我在其上运行了woorank报告。其报告的一个项目是:
www resolve Be careful! Your website without www doesn't redirect to www (or the opposite). It's duplicate content! Hide advice
High impactEasy to solve
Be sure that http://mysite.com and http://www.mysite.com are not running in parallel.
Redirecting requests from a non-preferred hostname is important because search engines consider URLs with and without "www" as two different websites.
Once your preferred domain is set, use a 301 redirect for all traffic to your non-preferred domain.
我在线阅读了一些帖子,并想知道在asp.net 4中修复此问题的 QUICK AND EASY 解决方案。
感谢。
答案 0 :(得分:4)
有完全相同的问题,用我的全局asax修复它 如果你要求我的网站没有www,我基本上会用301重定向你。顺便说一句,你很可能不需要if(url重写)的东西。只需要在其他地方完成工作。
void Application_BeginRequest(object sender, EventArgs e)
{
try
{
if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().StartsWith("http://mysite"))
{
string newUrl = string.Empty;
if (HttpContext.Current.Items["UrlRewritingNet.UrlRewriter.VirtualUrl"] != null)
newUrl = "http://www.mysite.com" + HttpContext.Current.Items["UrlRewritingNet.UrlRewriter.VirtualUrl"].ToString();
else
newUrl = HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Replace("http://mysite", "http://www.mysite");
Response.Status = "301 Moved Permanently";
Response.StatusCode = 301;
Response.StatusDescription = "Moved Permanently";
Response.AddHeader("Location", newUrl);
Response.End();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}