Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create SEO-Friendly urls in ASP.Net-MVC

Tags:

asp.net-mvc

I'm getting myself acquainted with ASP.Net-MVC, and I was trying to accomplish some common tasks I've accomplished in the past with webforms and other functionality. On of the most common tasks I need to do is create SEO-friendly urls, which in the past has meant doing some url rewriting to build the querystring into the directory path.

for example: www.somesite.com/productid/1234/widget

rather than: www.somesite.com?productid=1234&name=widget

What method do I use to accomplish this in ASP.Net-MVC?

I've search around, and all I've found is this, which either I'm not understanding properly, or doesn't really answer my question:

SEO URLs with ASP.NET MVC

like image 887
Ben Lesh Avatar asked Aug 25 '09 02:08

Ben Lesh


3 Answers

MVC stands for "Model View Controller" and while those concepts aren't what you're asking about, you generally can wire up URL's like you see above quite easily

So for example by default the URL's look like the following

http://www.somesite.com/controller/view/

where controller refers to the controller class within your project, and view refers to the page/method combination within the controller. So for example you could write the view to take in an input and look something like the following

http://www.somesite.com/widget/productid/1234/

Now as for SEO Friendly URL's, that's just useless sugar. You author your controller such that it adds harmless cruft to the end of the URL.

So for example, you'll notice that the following three ways to get to this question produce the same result:

How do I create SEO-Friendly urls in ASP.Net-MVC

How do I create SEO-Friendly urls in ASP.Net-MVC

How do I create SEO-Friendly urls in ASP.Net-MVC

Stack Overflow has authored their route values such that the bit that occurs after the question ID isn't really necessary to have.

So why have it there? To increase Google PageRank. Google PageRank relies on many things, the sum total of which are secret, but one of the things people have noticed is that, all other things being equal, descriptive text URL's rank higher. So that's why Stack Overflow uses that text after the question number.

like image 129
Tom Kidd Avatar answered Nov 10 '22 12:11

Tom Kidd


Create a new route in the Global.asax to handle this:

        routes.MapRoute(
            "productId",                  // Route name
            "productId/{id}/{name}",      // URL with parameters
            new { controller = "Home", action = "productId", id = 1234, name = widget }  // Parameter defaults
        );

Asp.Net MVC has routing built in, so no need for the Url Rewriter.

like image 40
Martin Avatar answered Nov 10 '22 10:11

Martin


Be careful when implementing routes with names in them, you need to validate that the name coming in is correct or you can end up harming your SEO-Ranking on the page by having multiple URIs share the same content, either set up a proper canonical or have your controller issue 301s when visiting the 'wrong' URI.

A quick writeup I did on the latter solution can be found at:
http://mynameiscoffey.com/2010/12/19/seo-friendly-urls-in-asp-net-mvc/

Some info on canonicals:
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html

like image 40
mynameiscoffey Avatar answered Nov 10 '22 10:11

mynameiscoffey