Convert non-ASCII characters (umlauts, accents…) to their closest ASCII equivalent (for slug creation)

The easiest way I’ve found: var str = “Rånades på Skyttis i Ö-vik”; var combining = /[\u0300-\u036F]/g; console.log(str.normalize(‘NFKD’).replace(combining, ”)); For reference see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize

How can I create a friendly URL in ASP.NET MVC?

There are two steps to solve this problem. First, create a new route or change the default route to accept an additional parameter: routes.MapRoute( “Default”, // Route name “{controller}/{action}/{id}/{ignoreThisBit}”, new { controller = “Home”, action = “Index”, id = “”, ignoreThisBit = “”} // Parameter defaults ) Now you can type whatever you want to … Read more

Can an “SEO Friendly” url contain a unique ID?

Be careful with allowing a page to render using the same method as Stack overflow. http://stackoverflow.com/questions/820493/random-text-can-cause-problems Black hats can this to cause duplicate content penalty for long tail competitors (trust me). Here are two things you can do to protect yourself from this. HTTP 301 redirect any inbound display url that matches your ID but … Read more

Why SlugField() in Django?

Why SlugField() in Django? Because: it’s human friendly (eg. /blog/ instead of /1/). it’s good SEO to create consistency in title, heading and URL. The big disadvantage of your dynamically generated slug, accepting slugs in urls.py and not using the slug to get the right object? It’s bad design. If you supply and accept slugs, … Read more

How to make Django slugify work properly with Unicode strings?

There is a python package called unidecode that I’ve adopted for the askbot Q&A forum, it works well for the latin-based alphabets and even looks reasonable for greek: >>> import unidecode >>> from unidecode import unidecode >>> unidecode(u’διακριτικός’) ‘diakritikos’ It does something weird with asian languages: >>> unidecode(u’影師嗎’) ‘Ying Shi Ma ‘ >>> Does this … Read more

Java code/library for generating slugs (for use in pretty URLs)

Normalize your string using canonical decomposition: private static final Pattern NONLATIN = Pattern.compile(“[^\\w-]”); private static final Pattern WHITESPACE = Pattern.compile(“[\\s]”); public static String toSlug(String input) { String nowhitespace = WHITESPACE.matcher(input).replaceAll(“-“); String normalized = Normalizer.normalize(nowhitespace, Form.NFD); String slug = NONLATIN.matcher(normalized).replaceAll(“”); return slug.toLowerCase(Locale.ENGLISH); } This is still a fairly naive process, though. It isn’t going to do … Read more

Replace all characters that aren’t letters and numbers with a hyphen [duplicate]

This should do what you’re looking for: function clean($string) { $string = str_replace(‘ ‘, ‘-‘, $string); // Replaces all spaces with hyphens. return preg_replace(‘/[^A-Za-z0-9\-]/’, ”, $string); // Removes special chars. } Usage: echo clean(‘a|”bc!@£de^&$f g’); Will output: abcdef-g Edit: Hey, just a quick question, how can I prevent multiple hyphens from being next to each … Read more

URL Slugify algorithm in C#?

http://predicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html public static string GenerateSlug(this string phrase) { string str = phrase.RemoveAccent().ToLower(); // invalid chars str = Regex.Replace(str, @”[^a-z0-9\s-]”, “”); // convert multiple spaces into one space str = Regex.Replace(str, @”\s+”, ” “).Trim(); // cut and trim str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); str = Regex.Replace(str, @”\s”, “-“); // hyphens return … Read more