Click here to Skip to main content
11,409,922 members (57,162 online)
Click here to Skip to main content

Really Easy URL-rewriting for SEO

, 6 Nov 2007
Rate this:
Please Sign up or sign in to vote.
An extremely simple way to rewrite URLs to increase SE page ranking


Search engines give lower rankings to pages with parameters in the URL, but without parameters, our data-driven websites would be useless indeed. Thus the need for URL-rewriting, so we can pass parameters using other means.


I originally tested out a few existing options, including one posted by Scott Guthrie, another one by Gaidar Magdanurov and a Code Project article by Manu Agrawal. In general, there were two issues that I encountered.

One, that the code/configuration required might not be so complex for rewriting the URL for a single ASPX page, but for a larger site with even ten ASPX pages requiring different parameters, these approaches required increasing amounts of code and configuration, either in the web.config file or in the code-behind for each ASPX page or both.

Two, many of the examples used the path separator ("/") to carry the parameters, such as http://[application root]/10/Customers.aspx to pass the customer ID "10". Of course, you're going to run into trouble accessing the CSS, JS and image files referenced in your code and even when those issues are resolved, none of those files will be cached because the client browser won't realize that the images are the same.

The solution is simple. First, let's use a dash ("-") instead of a slash ("/"). Adiós, caching issues! Second, let's pass the name of the parameters together with their values. That way, we can use one approach to URL-rewrite all of our pages. Bienvenido, scalability! If our customers page was normally accessed at customers.aspx?cid=10, then the new URL would be cid-10-customers.aspx. All it takes is a few lines in the global.aspx file.

The Code

In Application_BeginRequest, we have to parse the requested file (Request.CurrentExecutionFilePath). The only complex part is the regular expression I used to pull out the parameters, but a recursive method would have worked just as well. The Regex is complied to increase performance.

Regex regex = 
    new Regex("(?<name>[^-|/]+)-(?<value>[^-]+)",RegexOptions.Compiled);
MatchCollection matches = regex.Matches(Request.CurrentExecutionFilePath);

The regular expression simply matches for name and value pairs separated by dashes; it collects the names and values in named groups. If the match is successful, it means that the requested page is URL rewritten and we have to reconstruct the "real" path using a StringBuilder.

StringBuilder sb = new StringBuilder();

This gives us the name of the ASPX file. Now it's time to add the parameters...

foreach (Match match in matches) {
// remove final "&" character
sb.Remove(sb.Length - 1, 1);

That's it! Any ASPX page in your entire application can be referenced with SE-friendly URLs without having to write special code or configuration, or installing third-party components.

Points of Interest

  • At first, I tried using *.html as the file extension for URL-rewritten pages. All it took was a small substitution of "html" for "aspx" when reconstructing the original URL and for some reason I got a kick out of seeing ASP.NET-processed pages looking as if they were static HTML files! However, this approach didn't work on some servers that were configured to bypass ASP.NET for HTML files. If your application is hosted on a server that you don't have complete access to, it's better to stick with the *.aspx extension.
  • Obvious limitation: don't name any of your ASPX pages with a dash!


  • 6 November, 2007 -- Original version posted


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Bar Zecharya
Web Developer
Italy Italy
I started my software development career learning Perl and thought that all programming was as difficult as regular expressions! Then came C# and ASP.NET.

For the past five years I've been developing Windows- and Web-based applications in C#/ASP.NET and Java freelance and for various companies and international organizations in Italy and the USA, and in 2006 co-founded Rome-based Ibex Technologies Srl providing IT solutions involving software development, web design and network infrastructure.

Comments and Discussions

GeneralURL Re-Writer is not working in shared hosting. it takes user to 404 page not found Pinmembersundermagar22-Aug-09 5:58 
GeneralRe: URL Re-Writer is not working in shared hosting. it takes user to 404 page not found Pinmembersuzukisuv24-Aug-09 7:34 
GeneralMessage Automatically Removed PinmemberDotNetGuts24-Jul-08 11:34 
GeneralMessage Automatically Removed Pingroupyanglinghui16-Mar-11 4:20 
QuestionHow do I do??? Pinmemberspiderman_anhvu29-Jan-08 21:37 
AnswerRe: How do I do??? PinmemberBar Zecharya30-Jan-08 0:17 
QuestionRe: How do I do??? Pinmemberspiderman_anhvu31-Jan-08 16:16 
GeneralIs it possible to do this with your url rewritting Pinmemberjimkqui25-Nov-07 10:53 
GeneralRe: Is it possible to do this with your url rewritting PinmemberBar Zecharya25-Nov-07 23:18 
GeneralSEO Benefits PinmemberMark J. Miller13-Nov-07 5:08 
GeneralRe: SEO Benefits PinmemberBar Zecharya20-Nov-07 1:38 
GeneralRe: SEO Benefits PinmemberMark J. Miller20-Nov-07 5:37 
GeneralRegex is SLOW in C# PinmemberTomChantler13-Nov-07 1:05 
GeneralRe: Regex is SLOW in C# PinmemberMark J. Miller13-Nov-07 4:41 
GeneralRe: Regex is SLOW in C# PinmemberTomChantler13-Nov-07 5:16 
GeneralRe: Regex is SLOW in C# PinmemberMark J. Miller13-Nov-07 5:30 
GeneralProblem Pinmembermerlin9817-Nov-07 5:09 
GeneralRe: Problem PinmemberBar Zecharya7-Nov-07 5:23 
Generalgood fix Pinmemberguyinfun6-Nov-07 8:06 
GeneralRe: good fix PinmemberBar Zecharya7-Nov-07 14:37 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.5 | Last Updated 6 Nov 2007
Article Copyright 2007 by Bar Zecharya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid