I have a site on which I ran a woorank repor开发者_运维知识库t. One of the items it's reporting is:
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.
I read a few posts online and was wondering what QUICK AND EASY solution there is to fixing this in asp.net 4.
Thanks.
had exact same problem, fixed it with this in my global asax basically i redirect you with 301 if you ask for my site without the www. by the way you most likely wont need the if( url rewriting) stuff. just the line in the else with do the job.
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);
}
}
精彩评论