在SEO清洁Uri中删除的字符

Dan*_*Dan 2 c# asp.net seo uri

我正在使用asp.net/C#而我正在为我正在创建的小型CMS系统创建独特的(?)uris.

我正在从我的文章标题中生成uri片段,例如,如果标题是"我的神奇文章",则uri将是www.website.com/news/my-amazing-article

这有两个部分.首先,你认为我需要删除哪些角色?我用" - "替换空格,我想我也应该删除"/"字符.你能再想到可能导致问题吗?"?" 也许?我应该删除所有非字母字符吗?

第二个问题,上面我提到过uris可能需要独一无二.在添加之前我打算检查uri列表以确保唯一性,但是我看到堆栈溢出使用了一个数字加上一个uri.我假设允许标题重复?你认为这会是一个更好的方法吗?

Pie*_*ant 9

将所有变音符号转换为其基本字符,然后使用除去任何不是字母或数字的内容Char.IsLetterOrDigit.

然后用一个破折号替换所有空格.

这就是我们在软件中使用的内容.

/// <summary>
/// Convert a name into a string that can be appended to a Uri.
/// </summary>
private static string EscapeName(string name)
{
    if (!string.IsNullOrEmpty(name))
    {
        name = NormalizeString(name);

        // Replaces all non-alphanumeric character by a space
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < name.Length; i++)
        {
            builder.Append(char.IsLetterOrDigit(name[i]) ? name[i] : ' ');
        }

        name = builder.ToString();

        // Replace multiple spaces into a single dash
        name = Regex.Replace(name, @"[ ]{1,}", @"-", RegexOptions.None);
    }

    return name;
}

/// <summary>
/// Strips the value from any non english character by replacing thoses with their english equivalent.
/// </summary>
/// <param name="value">The string to normalize.</param>
/// <returns>A string where all characters are part of the basic english ANSI encoding.</returns>
/// <seealso cref="http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net"/>
private static string NormalizeString(string value)
{
    string normalizedFormD = value.Normalize(NormalizationForm.FormD);
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < normalizedFormD.Length; i++)
    {
        UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(normalizedFormD[i]);
        if (uc != UnicodeCategory.NonSpacingMark)
        {
            builder.Append(normalizedFormD[i]);
        }
    }

    return builder.ToString().Normalize(NormalizationForm.FormC);
}
Run Code Online (Sandbox Code Playgroud)

关于将这些生成的名称用作唯一ID,我会保证.使用生成的名称作为SEO帮助程序,但不能作为密钥解析程序.如果你看看stackoverflow如何引用他们的页面:

http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net
                                   ^--ID  ^--Unneeded name but helpful for bookmarks and SEO
Run Code Online (Sandbox Code Playgroud)

你可以在那里找到ID.这两个URL指向同一页面:

http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net

http://stackoverflow.com/questions/249087/
Run Code Online (Sandbox Code Playgroud)