In order to show an article or a news item or a blog post or any piece of information, you might be using a URL like the following:
.../ShowArticle.aspx?ID=c52703c5-3456-46aa-a59c-6a876e0aa9d4
You would have set up the ShowArticle.aspx page which parses out the query string and according to the ID parameter, you will go to the database and obtain the content and fill the fields on the aspx page. This works just fine; but the only problem is that the search engines don’t like this type of URLs.
Instead you want your url to be something like the following:
.../articles/generate-a-search-engine-friendly-string-instead-of-using-id.aspx
These names can be generated by using the titles of the news snippets or the blog posts themselves. Three things are done here:
- convert the string to lower case (looks nicer)
- take out all the non-alpha-numeric characters (both looks and need)
- replace the the whitespace with dashes (-)
Following is the code:
/// <summary>
/// Generates a search-engine-friendly url
/// </summary>
/// <param name="title">Regular title</param>
/// <returns>Friendly Name</returns>
///
private string GenerateName(string title)
{
// Convert the string into lower case
title = title.ToLower();
// Remove all the characters that are not alpha-numeric or spaces.
title = Regex.Replace(title, @"[^\w\s]", "");
// Replace whitespace characters (whitespace characters include spaces, tabs, etc.)
// with dashes (-)
title = Regex.Replace(title, @"\s+", "-");
return (title);
}