且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

来自数据库的漂亮网址

更新时间:2022-12-07 09:31:08

首先使用以下命令创建 .htaccess 文件以下内容:

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>

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

<?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 ,这反过来又可以解决做所需的事情。有3个重写条目;用于(符号)链接,文件和目录( / aaa 被视为文件,而 / aaa / bbb 被视为文件作为文件夹)

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)

路由器

$ _ SERVER ['REQUEST_URI'] 看起来像 / tag / 1 / slug 。我们首先用多余的斜杠修剪它,然后将其分解为3个项目(这样我们就不会影响子弹,它可能包含其他斜杠), print_r ing $ segments (对于标签/ 45 / Hello / World)如下所示:

$_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
)

最后,因为我看到您想重定向到 tags.php?id = 1 ,您要做的是设置 $ _ REQUEST ['id'] $ _ GET ['id'] 并加载 tags.php

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.