采用拼音 URL

文章目录

    为什么要采用拼音 URL

    • SEO 的考虑:目前的搜索引擎实际上很智能, 会自动把中文转换为拼音, URL 中的拼音也能被匹配上
    • 用户体验的考虑:从 URL 就能一目了然文章的内容

    如何定义 URL 的格式

    可以学习 stackoverflow 的做法:

    http://stackoverflow.com/questions/1101957/are-there-any-standard-exit-status-codes-in-linux

    学习 stackoverflow 的做法,实际上 id 已经能够代表这篇文章,但是
    stackoverflow 301 跳转到了新地址,即带描述的地址。

    $ curl -v http://stackoverflow.com/questions/1101957                                                                                                                                       
    * Hostname was NOT found in DNS cache
    * *   Trying 198.252.206.140...
    * * Connected to stackoverflow.com (198.252.206.140) port 80 (#0)
    * > GET /questions/1101957 HTTP/1.1
    * > User-Agent: curl/7.35.0
    * > Host: stackoverflow.com
    * > Accept: */*
    * > 
    * < HTTP/1.1 301 Moved Permanently
    * < Cache-Control: public, no-cache="Set-Cookie", max-age=60
    * < Content-Type: text/html; charset=utf-8
    * < Expires: Tue, 20 May 2014 06:14:35 GMT
    * < Last-Modified: Tue, 20 May 2014 06:13:35 GMT
    * < Location:
    /questions/1101957/are-there-any-standard-exit-status-codes-in-linux
    < Vary: *
    < X-Frame-Options: SAMEORIGIN
    < Set-Cookie: prov=4bab85c7-f446-463a-97e9-a6b834ad29ab;
    domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/;
    HttpOnly
    < Date: Tue, 20 May 2014 06:13:35 GMT
    < Content-Length: 185
    < 
    <html><head><title>Object moved</title></head><body>
    <h2>Object moved to <a
    href="/questions/1101957/are-there-any-standard-exit-status-codes-in-linux">here</a>.</h2>
    </body></html>
    * Connection #0 to host stackoverflow.com left intact
    

    代码实现

    可以使用 pypinyin 这个库结合 jieba 分词,将中文转换为拼音。

    需要注意的地方:

    • 中文标题中可能含有标点符号
    • 英文单词需要保留原样
    • 数字需要保留原样
    • 不同语境下的多音字需要能自动识别

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式