SEO: Duplicate URLs with and without dashes "/" and ASP.NET MVC

after reading this Slash or Not Slash article (link: http://googlewebmastercentral.blogspot.com/2010/04/to-slash-or-not-to-slash.html ) on the Google Webmaster Blog ( official) I decided to test my ASP.NET MVC application.

For example: http://domain.com/products and http://domain.com/products/ (with "/" at the end), return the code 200, which means: Google understands this as two different links and will probably be "duplicate content." They suggest choosing the method you want ... with or without a dash, and create 301 permanent redirects to your preferred path.

So, if I choose without a dash, when I try to access http://domain.com/products/ , it will return a 301 link without a dash: http://domain.com/products .

The question is, how can I do this with ASP.NET MVC?

Thanks Gui

+3
source share
3 answers

If you are using IIS 7, you can use the URL Rewrite Extension. ScottGu has a blog post about this here .

Alternatively, if you want to do this in code, you can inherit from PerRequestTask. Here's some sample code that removes www from an address - this is from 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-/ .

** , dll - System.Web.MVC.Eextensibility. **

+2

you need to check the URI in the INIT event and check the URI to see if it comes with a slash, if so, just do a redirect and add the 301 header to the output response.

0
source

Source: https://habr.com/ru/post/1743743/


All Articles