Best way to create SEO friendly URI string

前端 未结 1 1078
深忆病人
深忆病人 2021-02-14 19:02

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

What is the best way to make

1条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 19:19

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

    0 讨论(0)
提交回复
热议问题