duan1226 2016-02-20 01:43 采纳率: 100%
浏览 44

如何用url slug替换帖子ID

I have a DB table that saves my title as a slug (ie: this-is-my-title-slug) but I have no clue how to use it in the url.

Example ofor current url http://www.example.com/post.php?id=102

What I want is http://www.example.com/this-is-a-slug.

  • 写回答

1条回答 默认 最新

  • dongxiaoying5882 2016-02-20 01:58
    关注

    You're looking at SEO urls. And a slug is something you would use as a name of an article on www.example.com/articles/article-title-slug for instance, what you probably mean is a permalinks which basically are SEO urls.

    If you're running an apache server with PHP and you have a vhost setup then you can use that vhost to allow url rewriting (Apache mod_rewrite).

    This allows you to place a file called .htaccess in the webroot of your project (e.g. same directory as root index.php file), within this file you can set up rules that allow such rewriting. What often happens is that you rewrite everything to the root index.php file and you capture the URI and parse the parameters by splitting it on the forward slash (/) character. This is basically a router idea.

    The advantage of parsing the URI yourself is that you don't really have to do much in the .htaccess file which is much more complex to handle than building a router in PHP itself as well.

    A good example of a .htaccess file can be found on this SO answer

    about how to get going with that. There are also plenty of other tutorials to get started with this other than that answer.

    It's been a while since I've done it myself but if you have questions, just ask them and I'll see if I can help but anyways, this should point you in the right direction.

    评论

报告相同问题?

悬赏问题

  • ¥15 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?