【问题标题】:301 redirect for seoSEO 301 重定向
【发布时间】:2011-11-28 21:46:29
【问题描述】:

我有一个网站,我在该网站上运行了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 中有什么快速且简单的解决方案来解决这个问题。

谢谢。

【问题讨论】:

  • 抱歉,我看不到这个问题的编程方面。对我来说,这似乎更像是一个webmasters 问题。投票迁移。
  • 请参阅下面的答案。我认为这个问题会帮助不止一个人。

标签: asp.net seo


【解决方案1】:

有完全相同的问题,在我的全局 asax 中修复了它 基本上,如果您要求我的网站没有 www,我会使用 301 重定向您。顺便说一句,您很可能不需要 if( url rewriting) 的东西。只需 else 中的那一行就行了。

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);
            }
        }

【讨论】:

    猜你喜欢
    • 2010-11-15
    • 2012-12-28
    • 2014-12-28
    • 2016-06-21
    • 2014-09-19
    • 2023-04-05
    • 2011-05-23
    • 2021-02-04
    • 2013-06-22
    相关资源
    最近更新 更多