We help IT Professionals succeed at work.

How to mask and rewrite URLs in .NET for search engine optimization

423 Views
Last Modified: 2013-12-08
Hey guys. Here is what I am trying to accomplish. I have a fully database driven site written in .NET (ASP.NET front end, VB.NET backend). Because this client is very concerned about search engine optimization and visibility, I need to keep the URLs clean within the website, and make them product-centric.

In a nutshell, the average URL in the site looks like this:
http://myurl.com/products.aspx?cid=1&sid=1

Broken down, this means that the site will fetch main category 1 of products (cid) and subcategory 1 of products (sid). There are different levels of this and it goes into a bit more detail, but this is a good enough example for what I am trying to accomplish here.

Now if main product category 1 is listed by name in the database as Metal Widgets, and sub category 1 is listed by name as Blue Metal Widgets, for example, I want to rewrite the URL so it looks to the user (and more importantly the search engines) like:

http://myurl.com/metal-widgets/blue-metal-widgets

I need to basically make it verbal, and remove any .aspx filenames and replace the numeric id structure with its named counterpart.

I started doing some research on this, and started modifying the global.asax file to handle requests, but I am getting stuck. Can someone help me?

Pasted below is the function I started writing in the global.asax file (the commented out parts are pieces I couldnt figure out how to implement, that I copied from an example I found on the web):
protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        HttpContext incoming = HttpContext.Current;
        string oldpath = incoming.Request.Path.ToLower();
        string pageid; // page id requested

       // Regex regex1 = new Regex(@"page(\d+).aspx", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
        //MatchCollection matches1 = regex1.Matches(oldpath);

       // if (matches1.Count > 0)
       // {
            // It's a page view request.
            // Extract the page id and display the proper page.
          //  pageid = matches1[0].Groups[1].ToString();

            // We have the pageid. Now secretly redirect the request to the actual page.
            // The original URL in the web browser remains the same, but the content comes from the new URL.
          //  incoming.RewritePath("products.aspx?cid=" + pageid);
        //}
    }
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Im going to take a look at the link buddha sent, which hopefully will guide me (if I can't figure it out from there, I will post my difficulties here today!). However you are absolutely correct in what I am trying to do Werner, and I appreciate the advice and description. Im familiar with URL rewriting and translating (for lack of a better word) i've just never had to implement it, so I'm a bit of a noob at this.

Author

Commented:
Hi guys,

I checked through the link that you sent me, b u d d h a, and tried out the examples on ScottGu's blog. I just don't think I have the knowledge of what I am doing to incorporate this into my site. I have a feeling the solution I need will be one of the ones that listed on that link, but mixed in with a database table that references what to rewrite. Only problem is, I have NO idea how to combine these two into my site. There are hundreds of subcategories that will each need to be rewritten in their URL, so a hardcoded solution is not feasible, thus the database table.

I'm getting a better understanding of how the whole thing works, and I guess I'll just keep experimenting, but if anyone can provide any help a little bit more specific to exactly what I'm trying to do (taking into account I am really new at this particular genre), it would be appreciated.

Author

Commented:
Actually I may have just figured it out using the PathInfo method... have to do a bit more testing, but this might work. I'll post again for more help if I get stuck

Hello Nuvium,

Imfamiliar with URL rewriting and translating (for lack of a better word)i've just never had to implement it, so I'm a bit of a noob at this.

 <!-- richText -->Then, I have only one recommendation, but a golden one: whatever you design, think maintenance, maintenance, maintenance. Many "out of the box" rewrite kit for CMS (like Joomla) lead to defective website after few months of real life usage. By maintenance, I mean keeping the black-box running, but also keeping the website running...

There are hundreds of subcategories that will each need to be rewrittenin their URL, so a hardcoded solution is not feasible, thus thedatabase table.
I don't know your "product" database but I believe you still have the 2 options: include the actual values in the url (like /products/yellow-hammer,15,41.html ) or build a lookup table (database). For the database option, evaluate the sizing of it, and make sure to handle the case where the rewrote URL change over time.

I'm getting a better understanding of how the whole thing works, and Iguess I'll just keep experimenting, but if anyone can provide any helpa little bit more specific to exactly what I'm trying to do (takinginto account I am really new at this particular genre), it would beappreciated.
I'm not familiar with IIS/.asp (rather Apache/.php), but I'm familiar with URL-rewriting, and will be happy to help further if I can.

Rgds,

Werner.

Author

Commented:
I actually ended up using the URLRewriter module, but these got me looking in the right direction
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.