什么是slug URL 中的 slug

本文探讨了如何创建有意义且友好的URL结构,包括使用ID和文章标题作为URL的一部分。讨论了如何处理URL中的空格问题,并介绍了一种通过将空格替换为短横线并转换为小写字母的方法来生成友好的URL slug。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How would you reference this object with a URL, with a meaningful name? You could use Article.id so the URL would look like this:

www.example.com/article/23

Or, you could reference the title like so:

www.example.com/article/The 46 Year Old Virgin

Problem is, spaces aren't valid in URLs, they need to be replaced by %20 which is ugly, making it the following:

www.example.com/article/The%2046%20Year%20Old%20Virgin

That's not solving our meaningful URL. Wouldn't this be better:

www.example.com/article/the-46-year-old-virgin

That's a slug. the-46-year-old-virgin. All letters are downcased and spaces are replaced by hyphens -. See the URL of this very webpage for an example!

 

 $slug = url_title($this->input->post('title'), 'dash', TRUE);

用于将字符串 中的所有空格替换成连接符(-),并将所有字符转换为小写。 这样其实就生成了一个 slug ,可以很好的用于创建 URI 。

 

http://stackoverflow.com/questions/427102/what-is-a-slug-in-django

http://www.sjyhome.com/wordpress/wp-slug.html

 

转载于:https://www.cnblogs.com/bravehunter/p/5649606.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值