Best way to format pretty URLs for numeric IDs

后端 未结 10 1864
谎友^
谎友^ 2021-02-02 02:48

Alright, so let\'s say I\'m writing a forum application, and I want pretty URLs. However, all my tables use numeric IDs, so I\'m not sure the best way to format the URLs for tho

10条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 03:37

    I would go with option 3, and make the slug (the last bit) optional

    Because?

    • The ID will always be unique... 2 people may make a thread with the name 'good news' for example
    • The search bots can access the slug for some SEO goodness
    • The slug should be optional ... Using just the ID should still give you access to the site. Perhaps if the slug isn't there you could forward to the slug'd version, if you're concerned about duplicate content. You could always use the canonical meta tag to tell Google to index the slugged version.
    • Another benefit of the optional slug is if someone copies and pastes the URL into a document, there is a chance it could have characters at the end chopped off (because URLs generally don't have spaces, so they don't break to new lines). Having the slug optional means there is more of a chance people will find your page.

    I believe this is what Stack Overflow does.. and also notice they are doing rather well in the Search Engines.

    Update

    From the comments, be sure to 301 redirect any missing slug version to the correct slug.

提交回复
热议问题