Stack Overflow 如何生成对 SEO 友好的 URL? [英] How does Stack Overflow generate its SEO-friendly URLs?

查看:30
本文介绍了Stack Overflow 如何生成对 SEO 友好的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是好的完整正则表达式或其他一些可以使用标题的过程:

What is a good complete regular expression or some other process that would take the title:

如何像 Stack Overflow 一样将标题更改为 URL 的一部分?

How do you change a title to be part of the URL like Stack Overflow?

然后把它变成

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

用于 StackOverflow 上对 SEO 友好的 URL 中?

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

我使用的开发环境是Ruby on Rails,但如果有其他平台- 特定的解决方案(.NET、PHP、Django),我很想看到那些也是.

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.

推荐答案

这是我们的做法.请注意,边缘条件可能比您第一眼意识到的要多.

Here's how we do it. Note that there are probably more edge conditions than you realize at first glance.

这是第二个版本,展开后性能提高了 5 倍(是的,我对其进行了基准测试).我想我会优化它,因为这个函数每页可以调用数百次.

This is the second version, unrolled for 5x more performance (and yes, I benchmarked it). I figured I'd optimize it because this function can be called hundreds of times per page.

/// <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();
}

要查看此替换代码的先前版本(但功能等效,速度提高 5 倍),请查看此帖子的修订历史记录(单击日期链接).

To see the previous version of the code this replaced (but is functionally equivalent to, and 5x faster), view revision history of this post (click the date link).

此外,RemapInternationalCharToAscii 方法源代码可以在此处找到.

Also, the RemapInternationalCharToAscii method source code can be found here.

这篇关于Stack Overflow 如何生成对 SEO 友好的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆