Slugging it out with your URLs

I've been told that slugs are the very first thing read by search engines. They're that important.

To some, that statement sounds silly. You might be asking, "What does the common shell-less terrestrial gastropod mollusk have to do with the Internet?" Well, I'm not talking about that kind of slug. I'm talking about those pretty, easy-to-read URL's you see these days. If you clicked through to the specific article of this blog post (click on the title if you haven't already), the URL should be The "slugging-it-out-with-your-urls" part is a slug.

These days, content is king when it comes to search engine optimization (SEO). The more concise and relevent your content, the better chances you have of getting a higher ranking. Just as the title tag is important for on-site optimization, so are slugs. This is especially true because people will actually read the URL these days to determine if they want to visit a particular site. How many times have you searched for something like "best italian food in dallas, tx" and the first link in the results said "Mama Mia!" but the URL slug said, "20-things-not-to-say-to-your-wifes-face"? You probably wouldn't bother clicking through, right?

To create slugs, I wrote a very simple extension method to System.String called "ToSlug()." Here's the code:

public static String ToSlug(this String s)
    return Regex.Replace(Regex.Replace(Regex.Replace(s, @"[^a-zA-Z0-9\s-]", String.Empty), @"[\s-]+", " ").Trim(), @"\s", "-");

To use it, just add a using statement for whatever namespace is necessary (I put mine in a separate assembly - Core.Web - and reference it in a using statement as "Core.Web.Extensions"). To get the links in this blog to format with slugs, I do this:

<h1>@Html.ActionLink(blog.Title, "Article", "Blog", new { id = blog.Id, slug = blog.Title.ToSlug() }, new { title = String.Format("Permalink to {0}", blog.Title), rel = "Bookmark" })</h1>

I hope this helps!

Posted on 4/18/2013 at 11:04 PM , Edited on 4/19/2013 at 12:04 AM


Leave a comment