Click here to Skip to main content
11,480,626 members (63,178 online)
Click here to Skip to main content

Really Easy URL-rewriting for SEO

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

Introduction

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.

Background

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();
sb.Append(
    Request.CurrentExecutionFilePath.Substring(
        Request.CurrentExecutionFilePath.LastIndexOf('-')+1)
);

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

foreach (Match match in matches) {
      sb.Append(match.Groups["name"].Value)
      sb.Append("=");
      sb.Append(match.Groups["value"].Value);
      sb.Append("&");
}
// 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!

History

  • 6 November, 2007 -- Original version posted

License

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

Share

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 Pin
sundermagar22-Aug-09 5:58
membersundermagar22-Aug-09 5:58 
GeneralRe: URL Re-Writer is not working in shared hosting. it takes user to 404 page not found Pin
suzukisuv24-Aug-09 7:34
membersuzukisuv24-Aug-09 7:34 
GeneralMessage Automatically Removed Pin
24-Jul-08 11:34
memberDotNetGuts24-Jul-08 11:34 
GeneralMessage Automatically Removed Pin
16-Mar-11 4:20
groupyanglinghui16-Mar-11 4:20 
QuestionHow do I do??? Pin
spiderman_anhvu29-Jan-08 21:37
memberspiderman_anhvu29-Jan-08 21:37 
AnswerRe: How do I do??? Pin
Bar Zecharya30-Jan-08 0:17
memberBar Zecharya30-Jan-08 0:17 
QuestionRe: How do I do??? Pin
spiderman_anhvu31-Jan-08 16:16
memberspiderman_anhvu31-Jan-08 16:16 
GeneralIs it possible to do this with your url rewritting Pin
jimkqui25-Nov-07 10:53
memberjimkqui25-Nov-07 10:53 
GeneralRe: Is it possible to do this with your url rewritting Pin
Bar Zecharya25-Nov-07 23:18
memberBar Zecharya25-Nov-07 23:18 
GeneralSEO Benefits Pin
Mark J. Miller13-Nov-07 5:08
memberMark J. Miller13-Nov-07 5:08 
GeneralRe: SEO Benefits Pin
Bar Zecharya20-Nov-07 1:38
memberBar Zecharya20-Nov-07 1:38 
GeneralRe: SEO Benefits Pin
Mark J. Miller20-Nov-07 5:37
memberMark J. Miller20-Nov-07 5:37 
GeneralRegex is SLOW in C# Pin
TomChantler13-Nov-07 1:05
memberTomChantler13-Nov-07 1:05 
GeneralRe: Regex is SLOW in C# Pin
Mark J. Miller13-Nov-07 4:41
memberMark J. Miller13-Nov-07 4:41 
GeneralRe: Regex is SLOW in C# Pin
TomChantler13-Nov-07 5:16
memberTomChantler13-Nov-07 5:16 
GeneralRe: Regex is SLOW in C# Pin
Mark J. Miller13-Nov-07 5:30
memberMark J. Miller13-Nov-07 5:30 
GeneralProblem Pin
merlin9817-Nov-07 5:09
membermerlin9817-Nov-07 5:09 
GeneralRe: Problem Pin
Bar Zecharya7-Nov-07 5:23
memberBar Zecharya7-Nov-07 5:23 
Generalgood fix Pin
guyinfun6-Nov-07 8:06
memberguyinfun6-Nov-07 8:06 
GeneralRe: good fix Pin
Bar Zecharya7-Nov-07 14:37
memberBar 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.150520.1 | Last Updated 6 Nov 2007
Article Copyright 2007 by Bar Zecharya
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid