如何用url slug替换帖子ID

我有一个DB表,将我的标题保存为slug(即:this-is-my-title-slug )但我不知道如何在网址中使用它。 </ p>

当前网址示例 http:// www。 example.com/post.php?id=102 </ p>

我想要的是
http://www.example.com/this-is-a-slug 。</ p>
</ div>

展开原文

原文

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.

du7999
du7999 了解.htaccess
4 年多之前 回复

1个回答



您正在查看 SEO网址。 例如,你可以在www.example.com/articles/article-title-slug上使用一个slug作为文章的名称</ em>,你可能</ em>意味着什么 一个永久链接,基本上是SEO网址。</ p>

如果您正在运行带有PHP的apache服务器并且您有一个vhost设置,那么您可以使用该虚拟主机来允许 url重写(Apache mod_rewrite </ code>) </ em>。 </ p>

这允许您在项目的webroot中放置一个名为 .htaccess </ strong>的文件(例如,与root目录相同的目录 index.php </ strong> 文件),在此文件中,您可以设置允许此类重写的规则。 经常发生的是您将所有内容重写到根 index.php </ strong>文件中,并捕获URI并通过在正斜杠( / </ code>)字符上拆分它来解析参数。 这基本上是一个路由器的想法。</ p>

自己解析URI的好处是你不必在.htaccess文件中做太多事情,这个文件比构建要复杂得多 也是PHP本身的路由器。</ p>

可以在 SO回答 </ p>

关于如何开始使用它。 除了答案之外,还有许多其他教程可以开始使用。</ p>

展开原文

原文

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.

duangua6912
duangua6912 真棒! 谢谢(你的)信息。 关于成为新事物的部分原因是不知道该寻找什么。 谢谢
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐