Php 修改Mod_重写规则以将文章标题添加到URL

Php 修改Mod_重写规则以将文章标题添加到URL,php,mod-rewrite,Php,Mod Rewrite,我已经搜索过了,但是这里所有的例子都不适合我 I have a $_SESSION['title']; //Session variable contains title from calling page $title = $_SESSION['title']; //local variable contains article title 假设这篇文章的标题是《今日新闻》 如何将规则修改为 http://www.newstoday.com/readnews/12/news-of-the-

我已经搜索过了,但是这里所有的例子都不适合我

I have a $_SESSION['title']; //Session variable contains title from calling page

$title = $_SESSION['title']; //local variable contains article title
假设这篇文章的标题是《今日新闻》 如何将规则修改为

http://www.newstoday.com/readnews/12/news-of-the-day

BTW http://www.newstoday.com/readnews/12 //this works

RewriteEngine on
RewriteRule ^readnews/([0-9]+)$ readnews.php?news_art_id=$1

试试这样:

RewriteEngine on
RewriteRule ^readnews/([0-9]+)(/(.*))?$ readnews.php?news_art_id=$1&other_params=$3

稍后添加:id后面的所有内容都将位于其他参数GET param中。

您的正则表达式不正确。应该是:

RewriteEngine on
RewriteRule ^readnews/([0-9]+) readnews.php?news_art_id=$1

$表示URL的结尾,但是如果想要在id之后添加内容,则应将其删除。

首先,您需要确保PHP能够区分这两个URL

RewriteRule ^readnews/([0-9]+)(\/[a-z-]+)?$ readnews.php?news_art_id=$1&title=$2
这将使
$\u POST['title']
包含
当天新闻
,如果标题出现在URL中-否则
$\u POST['title']
将为空。在此之后,您只需检查它是否由PHP设置,如果不是,则重定向:

if (empty($_POST['title']))
  header("Location: /readnews/".$_POST['news_art_id']."/".$_SESSION['title']);

根据您的示例…像ths这样的锚定链接正确吗?…是的,
/readnews/12
/readnews/12/title
都应该可以工作。前者应该重定向到后者。我如何在html内的锚链接中使用它?您的两个URL都将重定向到readnews.php脚本,在那里您可以从$\u get['news\u art\u id']获得“12”,从$\u get['other\u params'获得“当天新闻”(第二种情况下是“”);从php生成此类链接:$id=0;//假设这是您的id$title=$\u会话['title']$链接=“”$id.“/”$标题