来自数据库的漂亮网址
我正在尝试在我的网站上获取漂亮的网址..现在它们看起来像这样:
www.site.com/tag.php?id=1
我想将其更改为
www.site.com/tag/1/slug
我的数据库表有 ID,标题,信息,Slug
我在网上阅读了有关 slug 的内容,但是对于 php 来说,没有运气,谁能帮我解决这个问题。
I am trying get pretty urls on my site..right now they look like this:
www.site.com/tag.php?id=1
I want to change that to
www.site.com/tag/1/slug
my database table has ID,Title,Info,Slug
I read online about slugs,but being new to php found no luck,can anyone help me with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先创建一个包含以下内容的
.htaccess
文件:然后,为
router.php
设置以下代码:进一步说明
Htaccess
htaccess 背后的概念非常简单。我们没有监听 URL 模式(如旧的 htaccess 软件所做的那样),而是简单地将所有可能导致 404 的流量重新路由到
router.php
,然后由 Router.php 负责执行所需的操作。有3个重写条目;用于(符号)链接、文件和目录(/aaa
被视为文件,而/aaa/bbb
被视为文件夹)路由器
$_SERVER['REQUEST_URI']
看起来像“/tag/1/slug”
。我们首先将其修剪为多余的斜杠,然后将其分解为 3 个项目(这样我们就不会影响可能包含其他斜杠的 slug),print_r
处理$segments
( for Tags/45/Hello/World) 看起来像:最后,因为我看到你想重定向到
tags.php?id=1
,所以你需要做的是设置$手动_REQUEST['id']
和$_GET['id']
并加载tags.php
。First create an
.htaccess
file with the following:Then, set the following code for
router.php
:Further Clarification
Htaccess
The concept behind the htaccess is very simple. Instead of listening for URL patterns (as old htaccess software did), we simply reroute all traffic that would otherwise result in a 404 to
router.php
, which in turn takes care of doing what is required. There are 3 rewrite entries; for (sym)links, files and directories (/aaa
is seen as a file while/aaa/bbb
is seen as a folder)Router
$_SERVER['REQUEST_URI']
looks like"/tag/1/slug"
. We first trim it against redundant slashes and then explode it in 3 items (so we don't affect slug, which might contain other slashes),print_r
ing the$segments
(for tags/45/Hello/World) would look like:Finally, since I see you want to redirect to
tags.php?id=1
, what you need to do is to set$_REQUEST['id']
and$_GET['id']
manually and loadtags.php
.尝试这个指南:
http://blogs.sitepoint.com/guide-url-rewriting/
// 不是我的博客...
或者这个看起来更好:
http://www.yourhtmlsource .com/sitemanagement/urlrewriting.html
编辑:
这是针对 apache 的,因为我认为这就是您正在使用的
try this guide:
http://blogs.sitepoint.com/guide-url-rewriting/
// not my blog...
or this which seems better:
http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html
EDIT:
this is for apache, as I assumed this was what you were using