来自数据库的漂亮网址

发布于 2024-11-03 18:52:15 字数 242 浏览 1 评论 0原文

我正在尝试在我的网站上获取漂亮的网址..现在它们看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静 2024-11-10 18:52:15

首先创建一个包含以下内容的 .htaccess 文件:

# Turn on rewrite engine and redirect broken requests to index
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* router.php [L,QSA]
</IfModule>

然后,为 router.php 设置以下代码:

<?php

$segments=explode('/',trim($_SERVER['REQUEST_URI'],'/'),3);

switch($segments[0]){
    case 'tag': // tag/[id]/[slug]
        $_REQUEST['id']=(int)$segments[1];
        $_GET['id']=(int)$segments[1];
        $slug=$segments[2];
        require_once('tag.php');
        break;
}

?>

进一步说明

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) 看起来像:

Array
(
    [0] => tag
    [1] => 45
    [2] => Hello/World
)

最后,因为我看到你想重定向到 tags.php?id=1,所以你需要做的是设置 $手动_REQUEST['id']$_GET['id']并加载tags.php

First create an .htaccess file with the following:

# Turn on rewrite engine and redirect broken requests to index
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* router.php [L,QSA]
</IfModule>

Then, set the following code for router.php:

<?php

$segments=explode('/',trim($_SERVER['REQUEST_URI'],'/'),3);

switch($segments[0]){
    case 'tag': // tag/[id]/[slug]
        $_REQUEST['id']=(int)$segments[1];
        $_GET['id']=(int)$segments[1];
        $slug=$segments[2];
        require_once('tag.php');
        break;
}

?>

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_ring the $segments (for tags/45/Hello/World) would look like:

Array
(
    [0] => tag
    [1] => 45
    [2] => Hello/World
)

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 load tags.php.

怪我鬧 2024-11-10 18:52:15

尝试这个指南:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文