SEO友好网址 [英] SEO Friendly URL
问题描述
我喜欢SO提出问题链接的方式
像这个问题一样,有链接http://stackoverflow.com/questions/6002203/seo-friendly-url
问题标题为seo-friendly-url
I love the way SO gives link to question
Like this question have the link http://stackoverflow.com/questions/6002203/seo-friendly-url
where the question title is seo-friendly-url
我正在创建一个博客,在该博客中,我想以同样的方式给出链接,如何在PHP中做到这一点?
任何建议都欢迎:)
I'm creating a blog where i want to give the link in the same way SO do, how to do that in PHP ?
Any suggestion is welcome :)
表结构
- ID
- 标题
- 标签
- 类别
- UID
Added
我正在使用PHP/APACHE,并且没有框架!我不想使用任何博客,想要创建自己的博客
推荐答案
我不确定为什么人们在这里故意如此钝化...
I'm not sure why people are being so deliberately obtuse here...
您正在寻找的是mod_rewrite
,这是用于URL重写的apache模块.
What you are looking for is mod_rewrite
, an apache module for URL rewriting.
在您的.htaccess文件中(您可能需要制作它):
In your .htaccess file (you might need to make it) put:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog\/([0-9]+)\/.*$ /blog.php?post=$1 [L]
</IfModule>
这意味着当您在幕后转到/blog/10/any-old-bit-of-text/
时,与您访问/blog.php?post=10
时是相同的.
This means when you go to /blog/10/any-old-bit-of-text/
behind the scenes it is identical to if you visited /blog.php?post=10
.
([0-9] +)位称为正则表达式(或regex),并且与任何数字匹配. .*
表示匹配任何内容. ^
锚定到查询的开始,而$
锚定到查询的结束.斜杠(/)以\/
转义.
The ([0-9]+) bit is called a regular expression (or regex), and matches any number. The .*
means match anything. The ^
anchors to the start of the query and the $
anchors to the end. slashes (/) are escaped as \/
.
这篇关于SEO友好网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!