创建 SEO 友好 URI 字符串的最佳方法

发布于 2024-10-10 05:16:00 字数 142 浏览 0 评论 0原文

该方法应仅允许 URI 字符串中包含“0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-”字符。

制作漂亮的 SEO URI 字符串的最佳方法是什么?

The method should allows only "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" chars in URI strings.

What is the best way to make nice SEO URI string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

躲猫猫 2024-10-17 05:16:00

这是普遍共识:

  1. 小写字符串。

    string = string.toLowerCase();
    
  2. 标准化所有字符并删除所有变音符号(例如 é、ö、à 变为 e、o、a)。

    string = Normalizer.normalize(string, Form.NFD).replaceAll("\\p{InCombiningDiariticMarks}+", "");
    

  3. - 替换所有剩余的非字母数字字符,并在必要时折叠。

    string = string.replaceAll("[^\\p{Alnum}]+", "-");
    

所以,总结一下:

public static String toPrettyURL(String string) {
    return Normalizer.normalize(string.toLowerCase(), Form.NFD)
        .replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
        .replaceAll("[^\\p{Alnum}]+", "-");
}

This is what the general consensus is:

  1. Lowercase the string.

    string = string.toLowerCase();
    
  2. Normalize all characters and get rid of all diacritical marks (so that e.g. é, ö, à becomes e, o, a).

    string = Normalizer.normalize(string, Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    
  3. Replace all remaining non-alphanumeric characters by - and collapse when necessary.

    string = string.replaceAll("[^\\p{Alnum}]+", "-");
    

So, summarized:

public static String toPrettyURL(String string) {
    return Normalizer.normalize(string.toLowerCase(), Form.NFD)
        .replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
        .replaceAll("[^\\p{Alnum}]+", "-");
}
葮薆情 2024-10-17 05:16:00

以下正则表达式将执行与您的算法相同的操作。我不知道有图书馆可以做这种事情。

String s = input
.replaceAll(" ?- ?","-") // remove spaces around hyphens
.replaceAll("[ ']","-") // turn spaces and quotes into hyphens
.replaceAll("[^0-9a-zA-Z-]",""); // remove everything not in our allowed char set

The following regex will do the same thing as your algorithm. I'm not aware of libraries for doing this type of thing.

String s = input
.replaceAll(" ?- ?","-") // remove spaces around hyphens
.replaceAll("[ ']","-") // turn spaces and quotes into hyphens
.replaceAll("[^0-9a-zA-Z-]",""); // remove everything not in our allowed char set
梦断已成空 2024-10-17 05:16:00

如果您想搜索更多信息,这些通常称为“slugs”。

您可能想查看其他答案,例如 如何从字符串创建 SEO 友好的短划线分隔网址?如何让 Django slugify 正确使用 Unicode 字符串?

它们比 javascript 更多地涵盖了 C# 和 Python,但对 slug 约定和您可能遇到的问题进行了一些与语言无关的讨论制作它们时(例如唯一性、unicode 规范化问题等)。

These are commonly called "slugs" if you want to search for more information.

You may want to check out other answers such as How can I create a SEO friendly dash-delimited url from a string? and How to make Django slugify work properly with Unicode strings?

They cover C# and Python more than javascript but have some language-agnostic discussion about slug conventions and issues you may face when making them (such as uniqueness, unicode normalization problems, etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文