【问题标题】:SEO: Duplicated URLs with and without dash "/" and ASP.NET MVCSEO:带有和不带有破折号“/”和 ASP.NET MVC 的重复 URL
【发布时间】:2011-02-14 20:32:30
【问题描述】:

在阅读了 Google Webmaster Central Blog(官方博客)上的这篇文章“Slash or not to slash”(链接:http://googlewebmastercentral.blogspot.com/2010/04/to-slash-or-not-to-slash.html)后,我决定测试我的 ASP.NET MVC 应用程序。

例如: http://domain.com/productshttp://domain.com/products/(末尾有“/”)返回代码 200,表示:Google 将其理解为两个不同的链接,很可能是“重复内容”。他们建议选择您想要的方式...带或不带破折号,并创建一个 301 永久重定向到首选方式。

因此,如果我选择不带破折号,当我尝试访问 http://domain.com/products/ 时,它将返回 301 到不带破折号的链接:http://domain.com/products

问题是,如何使用 ASP.NET MVC 做到这一点?

谢谢, 桂

【问题讨论】:

    标签: asp.net asp.net-mvc seo url-rewriting http-status-code-301


    【解决方案1】:

    如果您使用 IIS 7,您可以使用 URL 重写扩展 ScottGu 有一篇关于它的博客文章 here

    或者,如果您想在代码中执行此操作,您可以从 PerRequestTask 继承。这里有一些示例代码从地址中删除 www - 这是来自Shrinkr

    public class RemoveWww : PerRequestTask
    {
        protected override TaskContinuation ExecuteCore(PerRequestExecutionContext executionContext)
        {
            const string Prefix = "http://www.";
    
            Check.Argument.IsNotNull(executionContext, "executionContext");
    
            HttpContextBase httpContext = executionContext.HttpContext;
    
            string url = httpContext.Request.Url.ToString();
    
            bool startsWith3W = url.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase);
            bool shouldContinue = true;
    
            if (startsWith3W)
            {
                string newUrl = "http://" + url.Substring(Prefix.Length);
    
                HttpResponseBase response = httpContext.Response;
    
                response.StatusCode = (int) HttpStatusCode.MovedPermanently;
                response.Status = "301 Moved Permanently";
                response.RedirectLocation = newUrl;
                response.SuppressContent = true;
                response.End();
                shouldContinue = false;
            }
    
            return shouldContinue ? TaskContinuation.Continue : TaskContinuation.Break;
        }
    }
    

    您只需要检查代码中以 / 结尾的 url。

    ** 请注意,这确实使用了第 3 方 dll - System.Web.MVC.Extensibility 命名空间。 **

    【讨论】:

    • 我发现您发布的 Scott Gu 的链接非常有用。
    【解决方案2】:

    这对 Google 来说并不重要,但重要的是两个 url' http://domain.com/productshttp://domain.com/products/ 显示相同的页面,您还需要查看链接到您网站的 Windows 服务器,例如从用户输入的外部页面http://domain.com/PRODUCTS/ 也将被视为不同的页面,因为网络是案例敏感的。

    使用规范的 url 元标记绕过这一点,它告诉谷歌页面名称的真正含义,因此将避免蚂蚁真正重复的重复页面

    http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html

    【讨论】:

      【解决方案3】:

      您需要检查 INIT 事件中的 URI 并检查 URI 是否带有斜杠,如果是,只需执行重定向并将 301 标头添加到输出响应中。

      【讨论】:

      • 我应该在 Application_BeginRequest 上这样做吗?
      猜你喜欢
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 2018-07-12
      • 2020-07-07
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2012-11-19
      相关资源
      最近更新 更多