BeanSoftware Logo
 

 
ASP.NET Database Search Control
 
 

 
 Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

How to Create Slug from Page Title in ASP.NET?

Keywords in page URL are important for search engine optimization (SEO). Instead of meaningless URL with query strings in form like ShowArticle.aspx?id=23242234, it is better to create desctiptive file name with keywords separated by - character (slug), for example in form like Great-Keyword-Rich-Article.aspx. The technique used to provide keyword rich URLs is often URL rewriting. More about different options of implementation of URL rewriting you can read on URL Rewriting in ASP.NET tutorial.

Keyword rich file name, a.k.a. slug, can be written manually and this is sometimes enough if user is web developer who understands URL rules. But, if your site users are common people who just use your application you need to automate this task to avoid possible URL errors.

Most often, slug is based on page title, because by placing same keywords in page title and file name, you can get higher position in search results on Google and other search engines. Here is the code of title to slug method:

[ C# ]

/// <summary>
/// Creates article slug from title
/// </summary>
/// <param name="Title">Web page title</param>
/// <returns>Returns slug string with keywords separated by - character</returns>
/// <remarks></remarks>
public string CreateSlug(string Title)
{
 string Slug = Title.ToLower();
 // Replace characters specific fo croatian language
 // You don't need this part for english language
 // Also, you can replace other characters specific for other languages
 // e.g. é to e for French language etc.
 Slug = Slug.Replace("č", "c");
 Slug = Slug.Replace("ć", "c");
 Slug = Slug.Replace("š", "s");
 Slug = Slug.Replace("ž", "z");
 Slug = Slug.Replace("đ", "dj");
 
 // Replace - with empty space
 Slug = Slug.Replace("-", " ");
 
 // Replace unwanted characters with space
 Slug = Regex.Replace(Slug, "[^a-z0-9\s-]", " ");
 // Replace multple white spaces with single space
 Slug = Regex.Replace(Slug, "\s+", " ").Trim();
 // Replace white space with -
 Slug = Slug.Replace(" ", "-");
 
 Return Slug;
}

[ VB.NET ]

''' <summary>
''' Creates article slug from title
''' </summary>
''' <param name="Title">Web page title</param>
''' <returns>Returns slug string with keywords separated by - character</returns>
''' <remarks></remarks>
Public Function CreateSlug(ByVal Title As String) As String
 Dim Slug As String = Title.ToLower()
 ' Replace characters specific fo croatian language
 ' You don't need this part for english language
 ' Also, you can replace other characters specific for other languages
 ' e.g. é to e for French language etc.
 Slug = Slug.Replace("č", "c")
 Slug = Slug.Replace("ć", "c")
 Slug = Slug.Replace("š", "s")
 Slug = Slug.Replace("ž", "z")
 Slug = Slug.Replace("đ", "dj")
 
 ' Replace - with empty space
 Slug = Slug.Replace("-", " ")
 
 ' Replace unwanted characters with space
 Slug = Regex.Replace(Slug, "[^a-z0-9\s-]", " ")
 ' Replace multple white spaces with single space
 Slug = Regex.Replace(Slug, "\s+", " ").Trim()
 ' Replace white space with -
 Slug = Slug.Replace(" ", "-")
 
 Return Slug
End Function

So, in database's table you need two columns Title and Slug. When user creates new article call this function to create slug based on title. Be careful to create slug only first time, when article is published. If user decides to change article title later, slug should stay same because if anyone bookmarked that article before or there is an incoming link from other website, potential visitor will get only 404 Not Found Error when try to visit page with old URL.



Related articles:

1. URL Rewriting in ASP.NET

FAQ toolbar: Submit FAQ  |  Tell A Friend  |  Add to favorites  |  Feedback



Copyright © 2002-2008 Bean Software. All rights reserved.