用id和标题重写url(php)

用id和标题重写url(php),php,mod-rewrite,Php,Mod Rewrite,如何重写url(要做什么),以便: 它说: 使用php和mysql 谢谢。当使用GET of id=123访问示例article.php well页面时,检索数字的php代码为 $my\u id=$\u GET['id'] ,在这种情况下,将产生123 然后,您可以使用 header('Location: http://example.com/articles/$id/other stuff'); 我将把MySQL的内容留给其他人,但一个简单的教程将为您提供有关这方面的大量信息。要处理请求并重

如何重写url(要做什么),以便: 它说: 使用php和mysql
谢谢。

当使用GET of id=123访问示例article.php well页面时,检索数字的php代码为

$my\u id=$\u GET['id']

,在这种情况下,将产生123

然后,您可以使用

header('Location: http://example.com/articles/$id/other stuff');

我将把MySQL的内容留给其他人,但一个简单的教程将为您提供有关这方面的大量信息。

要处理请求并重定向到新url:

// get id of record from URL
$id = (int) $_GET['id'];
// suppose connection to database is established already
$result = mysql_query("SELECT `title` FROM `articles` WHERE `id` = $id");
if (!$result) {
    // there is no such an article
    // you can redirect to some other page, or show some error message
} else {
    $row = mysql_fetch_assoc($result);
    $title = $row['title'];
    $urlPart = generateURL($title);
    header('location:/articles/$id/$urlPart');
    exit;
}
您将需要
generateURL
函数,该函数接受字符串输入,并用
\uu
替换所有不可用的字符。(例如,如果你有标题“发生了什么事?”,它将取代
,并将空格替换为类似“正在发生什么”的内容)。您可以使用
str\u replace
preg\u replace
来实现此目的

如果您想在多个位置生成新的URL,可以将标题的URL格式直接作为另一列插入数据库
title\u URL
,然后只需选择它并使用它,而不是
title

SELECT `title_url` FROM `articles` WHERE `id` = $id

您无法在
.htaccess
中进行这种重写,因为您无法从该文件中连接到数据库服务器。(或者至少我不知道这样的解决方案)

看起来像是误解。我需要知道在.httaccess中键入什么来用something/idnumber/article_title替换something.php?id=你的意思是想做SEO重写吗?这通常意味着,当有人键入时,它被重定向到-此脚本然后在数据库中查找文章标题,以决定显示什么内容您希望将
/articles/article\u title
内部重写到
/article.php?id=article\u title
还是要重定向
/article.php?id=article\u title
我想是第二个。鼻涕虫,是吗?使用php mysql和其他任何需要的东西。