Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET
Article

Really Easy URL-rewriting for SEO

Rate me:
Please Sign up or sign in to vote.
4.71/5 (17 votes)
6 Nov 20073 min read 67.3K   468   59   26
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.

C#
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.

C#
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...

C#
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


Written By
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

 
GeneralGOOD Pin
Megha Rathii29-Sep-22 23:13
professionalMegha Rathii29-Sep-22 23:13 
QuestionGreat! Pin
Member 1392915717-Aug-18 8:03
Member 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
sundermagar22-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
suzukisuv24-Aug-09 6:34 
QuestionHow do I do??? Pin
Ng. Anh Vu29-Jan-08 20:37
Ng. Anh Vu29-Jan-08 20:37 
AnswerRe: How do I do??? Pin
Bar Zecharya29-Jan-08 23:17
Bar Zecharya29-Jan-08 23:17 
QuestionRe: How do I do??? Pin
Ng. Anh Vu31-Jan-08 15:16
Ng. Anh Vu31-Jan-08 15:16 
GeneralIs it possible to do this with your url rewritting Pin
jimkqui25-Nov-07 9:53
jimkqui25-Nov-07 9:53 
GeneralRe: Is it possible to do this with your url rewritting Pin
Bar Zecharya25-Nov-07 22:18
Bar Zecharya25-Nov-07 22:18 
GeneralSEO Benefits Pin
Mark J. Miller13-Nov-07 4:08
Mark J. Miller13-Nov-07 4:08 
GeneralRe: SEO Benefits Pin
Bar Zecharya20-Nov-07 0:38
Bar Zecharya20-Nov-07 0:38 
GeneralRe: SEO Benefits Pin
Mark J. Miller20-Nov-07 4:37
Mark J. Miller20-Nov-07 4:37 
GeneralRegex is SLOW in C# Pin
Tom Chantler13-Nov-07 0:05
professionalTom Chantler13-Nov-07 0:05 
GeneralRe: Regex is SLOW in C# Pin
Mark J. Miller13-Nov-07 3:41
Mark J. Miller13-Nov-07 3:41 
Yeah, but if you use the Compiled option and your regex is a static variable it won't affect performance. I've used a similar technique, with a much more complex regex on a site with over a million page views a day and 100,000 daily uniques. I was rewriting the URLs using regex on the way out (entire page) and when they came back in. Several times someone blamed this for performance issues, but when it was turned off there was no noticiable difference and it always turned out the performance issue was somewhere else.

I agree there is a performance overhead for regex, but I've yet to encounter a situation where it affected the user experience or system performance. In some cases using regex has helped increase performance over poorly writting string parsing functions and reduced code complexity at the same time. I love regex.


GeneralRe: Regex is SLOW in C# Pin
Tom Chantler13-Nov-07 4:16
professionalTom Chantler13-Nov-07 4:16 
GeneralRe: Regex is SLOW in C# Pin
Mark J. Miller13-Nov-07 4:30
Mark 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
Bar Zecharya7-Nov-07 4:23 
Generalgood fix Pin
hgrewa6-Nov-07 7:06
hgrewa6-Nov-07 7:06 
GeneralRe: good fix Pin
Bar Zecharya7-Nov-07 13:37
Bar 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.