Best way to create SEO friendly URI string

[亡魂溺海] 提交于 2019-12-04 19:17:38

问题


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

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


回答1:


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}]+", "-");
}



回答2:


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



回答3:


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).



来源:https://stackoverflow.com/questions/4581025/best-way-to-create-seo-friendly-uri-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!