Questions tagged [slug]

part of a URL that makes it more human readable or SEO-friendly, but without necessarily being required by the web server.

A slug is a part of a URL that makes it more human readable or SEO-friendly, but without necessarily being required by the web server. Slugs can be generated from a page title and are typically the last part of the url.

Example:

  • Page Title: c# - What's the difference between String and string? - Stack Overflow
  • Slug: whats-the-difference-between-string-and-string

The full URL including the slug is https://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string. Note that punctuation in the url is typically removed.

1108 questions
655
votes
11 answers

What is a "slug" in Django?

When I read Django code I often see in models what is called a "slug". I am not quite sure what this is, but I do know it has something to do with URLs. How and when is this slug-thing supposed to be used? (I have read its definition in this…
Jonas
  • 18,022
  • 9
  • 52
  • 67
336
votes
25 answers

Turn a string into a valid filename?

I have a string that I want to use as a filename, so I want to remove all characters that wouldn't be allowed in filenames, using Python. I'd rather be strict than otherwise, so let's say I want to retain only letters, digits, and a small set of…
Sophie Gage
  • 4,362
  • 4
  • 22
  • 21
264
votes
21 answers

How does Stack Overflow generate its SEO-friendly URLs?

What is a good complete regular expression or some other process that would take the title: How do you change a title to be part of the URL like Stack Overflow? and turn it into…
wusher
  • 11,851
  • 22
  • 66
  • 94
242
votes
3 answers

Remove all special characters from a string

I am facing an issue with URLs, I want to be able to convert titles that could contain anything and have them stripped of all special characters so they only have letters and numbers and of course I would like to replace spaces with hyphens. How…
user115422
  • 4,172
  • 8
  • 22
  • 36
228
votes
9 answers

How do I create a slug in Django?

I am trying to create a SlugField in Django. I created this simple model: from django.db import models class Test(models.Model): q = models.CharField(max_length=30) s = models.SlugField() I then do this: >>> from mysite.books.models import…
Johnd
  • 5,433
  • 9
  • 26
  • 22
186
votes
24 answers

PHP function to make slug (URL string)

I want to have a function to create slugs from Unicode strings, e.g. gen_slug('Andrés Cortez') should return andres-cortez. How should I do that?
Andres SK
  • 10,054
  • 20
  • 85
  • 138
112
votes
14 answers

Why do some websites add "Slugs" to the end of URLs?

Many websites, including this one, add what are apparently called slugs - descriptive but as far as I can tell useless bits of text - to the end of URLs. For example, the URL the site gives for this question…
Dave Webb
  • 179,733
  • 56
  • 298
  • 296
106
votes
10 answers

String slugification in Python

I am in search of the best way to "slugify" string what "slug" is, and my current solution is based on this recipe I have changed it a little bit to: s = 'String to slugify' slug = unicodedata.normalize('NFKD', s) slug = slug.encode('ascii',…
Zygimantas
  • 6,047
  • 7
  • 36
  • 51
88
votes
5 answers

URL Slugify algorithm in C#?

So I have searched and browsed through the slug tag on SO and only found two compelling solution: Slugify and Character Transliteration in C# How to convert super- or subscript to normal text in C# Which are but partial solution to the problem. I…
chakrit
  • 57,172
  • 24
  • 125
  • 160
66
votes
12 answers

Best way to generate slugs (human-readable IDs) in Rails

You know, like myblog.com/posts/donald-e-knuth. Should I do this with the built in parameterize method? What about a plugin? I could imagine a plugin being nice for handling duplicate slugs, etc. Here are some popular Github plugins -- does anyone…
Tom Lehman
  • 75,197
  • 69
  • 188
  • 262
43
votes
3 answers

Why SlugField() in Django?

Django has models.SlugField() which helps us to create some cool looking urls. My question is why specifying it as a field suppose I have this model class Blog(models.Model): title = models.CharField() and if I want to add slug, I could just…
Ryu_hayabusa
  • 3,238
  • 1
  • 26
  • 31
42
votes
5 answers

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

Web frameworks such as Rails and Django has built-in support for "slugs" which are used to generate readable and SEO-friendly URLs: Slugs in Rails Slugs in Django A slug string typically contains only of the characters a-z, 0-9 and - and can hence…
knorv
  • 45,461
  • 71
  • 205
  • 289
41
votes
8 answers

How to make Django slugify work properly with Unicode strings?

What can I do to prevent slugify filter from stripping out non-ASCII alphanumeric characters? (I'm using Django 1.0.2) cnprog.com has Chinese characters in question URLs, so I looked in their code. They are not using slugify in templates, instead…
Imran
  • 76,055
  • 23
  • 93
  • 124
39
votes
3 answers

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

How do I generate friendly URLs within the ASP.NET MVC Framework? For example, we've got a URL that looks like this: http://site/catalogue/BrowseByStyleLevel/1 The 1 is Id of the study level (Higher in this case) to browse, but I'l like to reformat…
Kieron
  • 24,968
  • 14
  • 72
  • 113
39
votes
6 answers

Is there an easy way to populate SlugField from CharField?

class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to get the slug field to autopopulate based on the title? Perhaps in the Admin and outside of the Admin.
ashchristopher
  • 21,525
  • 16
  • 44
  • 49
1
2 3
73 74