Click here to Skip to main content
14,302,993 members

Really Easy URL-rewriting for SEO

Rate this:
4.70 (14 votes)
Please Sign up or sign in to vote.
4.70 (14 votes)
6 Nov 2007
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

QuestionGreat! Pin
Member 1392915717-Aug-18 8:03
memberMember 1392915717-Aug-18 8:03 
GeneralURL Re-Writer is not working in shared hosting. it takes user to 404 page not found Pin
sundermagar22-Aug-09 4:58
membersundermagar22-Aug-09 4:58 
GeneralRe: URL Re-Writer is not working in shared hosting. it takes user to 404 page not found Pin
suzukisuv24-Aug-09 6:34
membersuzukisuv24-Aug-09 6:34 
QuestionHow do I do??? Pin
Ng. Anh Vu29-Jan-08 20:37
memberNg. Anh Vu29-Jan-08 20:37 
AnswerRe: How do I do??? Pin
Bar Zecharya29-Jan-08 23:17
memberBar Zecharya29-Jan-08 23:17 
QuestionRe: How do I do??? Pin
Ng. Anh Vu31-Jan-08 15:16
memberNg. Anh Vu31-Jan-08 15:16 
GeneralIs it possible to do this with your url rewritting Pin
jimkqui25-Nov-07 9:53
memberjimkqui25-Nov-07 9:53 
GeneralRe: Is it possible to do this with your url rewritting Pin
Bar Zecharya25-Nov-07 22:18
memberBar Zecharya25-Nov-07 22:18 
GeneralSEO Benefits Pin
Mark J. Miller13-Nov-07 4:08
memberMark J. Miller13-Nov-07 4:08 
GeneralRe: SEO Benefits Pin
Bar Zecharya20-Nov-07 0:38
memberBar Zecharya20-Nov-07 0:38 
GeneralRe: SEO Benefits Pin
Mark J. Miller20-Nov-07 4:37
memberMark J. Miller20-Nov-07 4:37 
GeneralRegex is SLOW in C# Pin
Tom Chantler13-Nov-07 0:05
memberTom Chantler13-Nov-07 0:05 
GeneralRe: Regex is SLOW in C# Pin
Mark J. Miller13-Nov-07 3:41
memberMark J. Miller13-Nov-07 3:41 
GeneralRe: Regex is SLOW in C# Pin
Tom Chantler13-Nov-07 4:16
memberTom Chantler13-Nov-07 4:16 
GeneralRe: Regex is SLOW in C# Pin
Mark J. Miller13-Nov-07 4:30
memberMark J. Miller13-Nov-07 4:30 
GeneralProblem Pin
merlin9817-Nov-07 4:09
professionalmerlin9817-Nov-07 4:09 
GeneralRe: Problem Pin
Bar Zecharya7-Nov-07 4:23
memberBar Zecharya7-Nov-07 4:23 
Generalgood fix Pin
hgrewa6-Nov-07 7:06
memberhgrewa6-Nov-07 7:06 
GeneralRe: good fix Pin
Bar Zecharya7-Nov-07 13:37
memberBar Zecharya7-Nov-07 13:37 

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

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

Posted 6 Nov 2007


58 bookmarked