views:

84

answers:

3

I am building a product catalog for a customer website under ASP.NET using .NET Framework 3.5 SP1. Each product has a part number and an OEM part number (all globally unique).

For SEO purposes I would like the OEM part number to be as close as possible to the actual domain name.

How do I build a routing rule that allows me to do this:

http://www.myDomain.com/oemPartNumber
http://www.myDomain.com/myPartNumber

while still being able to do this:

http://www.myDomain.com/welcome
http://www.myDomain.com/products
http://www.myDomain.com/services
http://www.myDomain.com/contact

I would also love to hear your other SEO suggestions (we are primarily interested in Google) if you have any.

Thanks.

IMPORTANT: This not an MVC site, so I don't have controllers.

+1  A: 

If there are specific formats to the part numbers you can use regex constraints on the route like this:

routes.MapRoute(
    "Part number",
    "{partNumber}",
    new { controller = "Part", action = "Display" },
    new
    {
        partNumber = @"\d+" // part number must be numeric
    }
);

Text like "welcome" won't match the regex and so will ignore this route.

Garry Shutler
This is not an MVC site, so I don't have controllers.
Robert Harvey
But you can use the routing mechanism along with the route constraints without MVC http://www.codeproject.com/KB/aspnet/url_rewriting_routing.aspx
Garry Shutler
+1 Thanks for the Code Project Link.
Robert Harvey
+1  A: 

You should be able to specify something like http://www.mydomain.com/oempartnumber/oem and http://www.mydomain.com/mypartnumber/pn. There must be something in the url that allows you to choose the controller you want to use and further more allow you to distinguish between a part number and an oem part number (unless those are also unique against one another. If there will never be overlap between oem and pn then you could have http://www.mydomain.com/{partnumber}/pn.

RouteTable.Routes.Add(new Route
{
Url = "[query]/pn",
Defaults = new { controller="PartNumber", action = "Details" },
RouteHandler = typeof(MvcRouteHanderl)
});

You could use some trickery with a route like this:

routes.MapRoute(
    "Part number",
    "{partNumber}",
    new { controller = "Part", action = "Display" },
    new
    {
        partNumber = @"\d+" // part number must be numeric
    }
);

But the problem here is that an OEM part number that is not actually a part number (such as "ave-345") would not match!

UPDATE: In reading I noticed that you said "this is not an MVC site so I don't have controllers!"...OH! That changes things. In that case you can check to see if the directory exists where you pass in http://www.mydomain.com/1234 and if not you can test it for a product number. This would have to be done in a HttpModule though so you can catch it before your page is executed. Then on the server side you can direct the page to http://www.domain.com/productdetails?pid=1234.

Take a look here to understand that: http://www.15seconds.com/Issue/020417.htm

For this you will have a class that inherits from IHttpModule. Then you can specify an Init method

public void Init(HttpApplication application)
{
    //let's register our event handler
    application.PostResolveRequestCache +=
     (new EventHandler(this.Application_OnAfterProcess));
}

This then points to your Applicaton_OnAfterProcess method:

private void Application_OnAfterProcess(object source, EventArgs e)

{
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;

    ...

Inside of here you can specify some rules about what you are looking for.

I usually do something along the lines of

if (!System.IO.File.Exists(application.Request.PhysicalPath)) //doesn't exist
{
    //you test for your product ID here
    ...
    //if you find it stuff it into a ProductID variable for later...

Once you isolate your product ID you can then rewrite the URL (server side) and direct the user to the proper productDetails.aspx page.

context.RewritePath("~/products/productDetails.aspx?ProductID=" + ProductID.ToString());

So while the user and google sees http://www.mydomain.com/1234 your application will see http://www.mydomain.com/products/productdetails.aspx?productid=1234 and you can code against it as usual.

I hope this is what you were looking for instead!

Andrew Siemer
This is not an MVC site, so I don't have controllers.
Robert Harvey
Take a look at my update! It addresses the same need for a non mvc site with aspx and HttpModules. You will need to create the concept of a controller, intercept the request, and transform it from mydomain.com/123456 to mydomain.com/products/productdetails.aspx?pid=123456 on the server side.
Andrew Siemer
+1 This looks very useful. It doesn't look anything like I thought it would look, so I'm glad I asked the question. Thanks, I will let you know how it goes!
Robert Harvey
+1  A: 

Hi Robert,

I see that you have already accepted an answer, but let me post a new one that is simpler.

This not an MVC site, so I don't have controllers.

If you use Web Forms, this is for you: Using Routing with Web Forms

Basically, you have to create your own IRouteHandler and set up your routes to it. Then, you can use it to map as many URLs as you want to your Web Forms. This saves you from the old URL rewriting method and such dirty tricks. (The download link on that blog entry doesn't work anymore, but this is pretty simple to imlement. Anyways, if you need a working example, I can provide you with one.)

Venemo