How does Stack Overflow generate its SEO-friendly URLs

0 votes

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

how-do-you-change-a-title-to-be-part-of-the-url-like-stack-overflow

that is used in the SEO-friendly URLs on Stack Overflow?

The development environment I am using is Ruby on Rails, but if there are some other platform-specific solutions (.NET, PHP, Django), I would love to see those too.

I am sure I (or another reader) will come across the same problem on a different platform down the line.

I am using custom routes, and I mainly want to know how to alter the string to all special characters are removed, it's all lowercase, and all whitespace is replaced.

Feb 27, 2022 in Others by Kichu
• 19,040 points
1,426 views

1 answer to this question.

0 votes
/// <summary>
/// Produces optional, URL-friendly version of a title, "like-this-one". 
/// hand-tuned for speed, reflects performance refactoring contributed
/// by John Gietzen (user otac0n) 
/// </summary>
public static string URLFriendly(string title)
{
    if (title == null) return "";

    const int maxlen = 80;
    int len = title.Length;
    bool prevdash = false;
    var sb = new StringBuilder(len);
    char c;

    for (int i = 0; i < len; i++)
    {
        c = title[i];
        if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))
        {
            sb.Append(c);
            prevdash = false;
        }
        else if (c >= 'A' && c <= 'Z')
        {
            // tricky way to convert to lowercase
            sb.Append((char)(c | 32));
            prevdash = false;
        }
        else if (c == ' ' || c == ',' || c == '.' || c == '/' || 
            c == '\\' || c == '-' || c == '_' || c == '=')
        {
            if (!prevdash && sb.Length > 0)
            {
                sb.Append('-');
                prevdash = true;
            }
        }
        else if ((int)c >= 128)
        {
            int prevlen = sb.Length;
            sb.Append(RemapInternationalCharToAscii(c));
            if (prevlen != sb.Length) prevdash = false;
        }
        if (i == maxlen) break;
    }

    if (prevdash)
        return sb.ToString().Substring(0, sb.Length - 1);
    else
        return sb.ToString();
}


this is the second version of the code and have worked so far and it has 5x more performance boosted 

answered Feb 28, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

How to use $_GET parameter to make seo friendly urls?

What you can do is the following RewriteEngine ...READ MORE

answered Feb 27, 2022 in Others by narikkadan
• 63,600 points
6,216 views
0 votes
1 answer

What are canonical URLs and how do they affect your SEO?

canonical URLs are distinct URL used to ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,600 points
486 views
0 votes
1 answer

Creating SEO friendly urls using htaccess

its just a simple step just use ...READ MORE

answered Feb 20, 2022 in Others by narikkadan
• 63,600 points
646 views
0 votes
1 answer

Googlebot is accessing .aspx pages, it should access SEO-friendly URLs only

the url mapping would be wrong that ...READ MORE

answered Feb 20, 2022 in Others by narikkadan
• 63,600 points
499 views
0 votes
1 answer

Generate SEO friendly URLs- Slugs

// source: https://code.google.com/archive/p/php-slugs/ function my_str_split($string) { ...READ MORE

answered Feb 22, 2022 in Others by narikkadan
• 63,600 points
573 views
0 votes
1 answer

Googlebot is accessing .aspx pages, it should access SEO-friendly URLs only

google bot will find  it if you ...READ MORE

answered Feb 11, 2022 in Others by narikkadan
• 63,600 points
488 views
0 votes
1 answer

Can an "SEO Friendly" url contain a unique ID?

beware you can get penalised for duplicate ...READ MORE

answered Feb 17, 2022 in Others by narikkadan
• 63,600 points
556 views
0 votes
1 answer

Better SEO to remove "stop" words from an article's URL Slug?

these words doesnt have much priority in ...READ MORE

answered Feb 20, 2022 in Others by narikkadan
• 63,600 points
557 views
0 votes
1 answer

Generate SEO friendly URLs (slugs)

function format_uri( $string, $separator = '-' ) {     $accents_regex ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,600 points
613 views
0 votes
1 answer

How can I create custom SEO-friendly URLs in OpenCart?

Index: catalog/controller/common/seo_url.php =================================================================== --- catalog/controller/common/seo_url.php (old) +++ catalog/controller/common/seo_url.php ...READ MORE

answered Feb 21, 2022 in Others by narikkadan
• 63,600 points
1,048 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP