Click here to Skip to main content
Licence CPOL
First Posted 24 Dec 2006
Views 31,422
Downloads 330
Bookmarked 35 times

Using URL rewriting to make money from Google and other search engines

By | 24 Dec 2006 | Article
Easy way in ASP.NET to do MOD URL base from a table in the database.

Introduction

This article explains an easy way in ASP.NET to do a MOD URL base from a table in the database.

Package contents:

  • URL Rewriter solution
  • Sample Web Project
  • Table scripts
  • Web.Config file
  • SQL scripts to create a Stored Procedure and database tables

You can create a URL rewriting component for ASP.NET.

The two site examples are:

Initial Problem

I needed a way to create nice URLs that search engines like for a content page. The goal of this was to be able to add content pages to a site and automatically have the URL rewritten and pulled from a database.

The content site has an article summary page and an article detail page. Without URL rewriting, the links, as the search engine would see them, are:

  • http://bongofdestiny.com/GenericContentHandler.aspx?ContentID=35
  • http://bongofdestiny.com/GenericContentHandler.aspx?ContentID=33

or

  • http://www.collegecupcakes.com/index.aspx?CurrentImageID=211
  • http://www.collegecupcakes.com/GenericContentHandler.aspx?ContentID=6

A couple reasons we want to change this:

  • Search engines like words.
  • It is not apparent to the user what the link is.
  • If you email the link, it may be truncated in the email.

Look how much better these URLs look:

  • http://www.collegecupcakes.com/Carol-211-Fun.aspx
  • http://www.collegecupcakes.com/Party-With-No-Hangover.aspx

Or

  • http://bongofdestiny.com/Tenacious-D-Explosivo-Video.aspx
  • http://bongofdestiny.com/Tenacious-D-Song-Lyrics.aspx

That is the goal of this project.

Note: Much thanks to Scott Mitchell from 4GuysFromRolla.com. He created the base set of code that does the hard work. I just extended it to work with a database.

This is the original article this is based on:

Now, let's see how to do this.

The Solution

Step 1

Add a reference to URLWriter.dll to your ASP.NET project (1.0 or 2.0 doesn't matter). It is already compiled and ready to go in the download.

Step 2

Add the following to your web.config. This tells ASP.NET to send the request through the URLWriter etc.

<configSections>
<!-- The <configSections> element must contain a <section> tag 
         for the <RewriterConfig> 
         section element. The type of the section handler 
         is RewriterConfigSerializerSectionHandler, 
         which is responsible for deserializing 
         the <RewriterConfig> section element 
         into a RewriterConfig instance... -->
  <section name="RewriterConfig" 
     type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
</configSections>

The database connection to look up the URLs in a table:

</appSettings>
<add key="MODURLDB" 
 value="Data Source=[YourSQLServer];Initial Catalog=[DataBaseName];
        User ID=[YourUserID];Password=[YourPassword]"/>
</appSettings>

This tells ASP.NET what to use for the HTTP modules:

<httpModules>
  <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter"/>
</httpModules>

There is a sample web.config in the download for this source code.

Step 3

Create the tables in the database using the script URLWriterDBTableScripts.sql. This creates two tables and Stored Procedures. The Stored Procedures are called by the URLWriter.dll.

The page table is "PageMod" which lists the "LookFor" and "SendTo". When a request is made, the HTTP handlers will look for the URL in the dataset, and if it finds it, it sends it to "sendTo". If it doesn't find it, it passes the request on normally.

This creates two tables:

  • PageMod: holds the "LookFor" and "SendTo" columns for the URL rewriter engine.
  • PageContentLive: This table holds the content for the site. Notice the "PageMod" column, and the keywords, description.

This is a template table, so you can create a page that has a list of the links, to the content, and display the links to the user. One trick is to have a trigger on the "On Insert" event of a record, to auto populate the records in the PageMod table.

Step 4

Set the connection string in the web.config file for the URL Rewriter. That is the basic setup. If you want to look at the code, have a look at it. Let me know if there are any changes that would make it better.

Now, here is what you need to put into the table to test. The syntax must be exact:

When the request comes in for:

  • http://bongofdestiny.com/Tenacious-D-Lyrics-Kyle-Quit-The-Band.aspx
  • or

  • http://www.collegecupcakes.com/Carol-211-Fun.aspx

It is sent to:

  • http://bongofdestiny.com/GenericContentHandler.aspx?ContentID=43
  • or

  • http://www.collegecupcakes.com//index.aspx?CurrentImageID=211

So in the table, you would have:

[SAMPLE LOOK FOR ROOT] 
~/Tenacious-D-Lyrics-Kyle-Quit-The-Band\.aspx
or
~/Carol-211-Fun\.aspx 

The final slash before the .aspx is so the Regular Expression engines can quickly look for it.

[SAMPLE SEND TO ROOT]
~/GenericContentHandler.aspx?ContentID=43
or
~/index.aspx?CurrentImageID=211 

So that is the basics of how the thing works. This is my first article, so please let me know if it all works.

Source code information

In the source code:

  • RewriterConfiguration.cs: GetConfig: This is where the settings are grabbed from the database.
  • This is the data access component to access the database: MODDALC.cs.
  • Appconfig setting: MODURLDB.

Let me know if this helps and what we can do to extend the functionality.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ropel



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhat happens when there is a request of 4000 per second Pinmemberalbert arul prakash2:44 14 Jun '10  
QuestionIs it possible to cache the dataset? PinmemberSteveD11:04 26 Mar '09  
GeneralFor Simplest Way of writing URL Rewriting PinmemberDotNetGuts10:40 24 Jul '08  
GeneralThanks PinmemberGh0stman4:48 16 Jan '07  
QuestionWhat the hell is the use of that PinmemberThe Pain Killer20:01 24 Dec '06  
AnswerRe: What the hell is the use of that PinmemberHarry Cruz23:59 24 Dec '06  
AnswerRe: What the hell is the use of that Pinmemberropel17:05 25 Dec '06  
GeneralRe: What the hell is the use of that PinmemberJeff Bowman17:20 25 Dec '06  
AnswerRe: What the hell is the use of that Pinmemberropel17:19 25 Dec '06  
GeneralRe: What the hell is the use of that Pinmemberdarrenmm0:53 26 Dec '06  
GeneralRe: What the hell is the use of that PinmemberDoug K. Wilson8:58 26 Dec '06  
GeneralRe: What the hell is the use of that Pinmemberdarrenmm9:38 26 Dec '06  
The only problem with ISAPI ReWrite is that I'd like to store the Vanity URL's in the database and without access to the code, I'm stuck with the META base storage. The main goal would be to create a user interface where a user can put in two values:
 
1. Page Name: http://www.domain.com/default.aspx?id-6
2. Vanity URL: http://www.domain.com/games/
 
The module would pull off the HOST ("domain.com") and URI ("/game/"), then do a lookup and get the Page to redirect the user too. The nice part about database enabling this is that I don't need to touch IIS, it's META base or anything else - it becomes a single point of storage for all vanity URL's (especially when you are running in a web farm).
 
There are other hacks around this, if you can afford an F5, you can use the load balancer iRule engine to handle the URL re-writing, but it seems ridiculous for something so trivial. Does anyone know if IIS7 will support this functionality our of the box? Maybe a provider model??

GeneralRe: What the hell is the use of that Pinmemberropel13:24 4 Jan '07  
GeneralRe: What the hell is the use of that PinmemberLord Delacroix20:12 9 Mar '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 24 Dec 2006
Article Copyright 2006 by ropel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid