2011-07-13 32 views
1

我已经搜索过,但是这里的所有例子都不适合我。修改Mod_rewrite规则,将文章标题添加到URL

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 

回答

1

尝试类似的东西:

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

添加稍后:id后面的所有内容都将在other_params GET参数中。

+0

我该如何在html链接中使用此链接? –

+0

这两个你的网址 http://www.newstoday.com/readnews/12/news-of-the-day http://www.newstoday.com/readnews/12 将重定向到readnews.php脚本,您可以从$ _GET ['news_art_id']和“$ news”(对于第二种情况是“”)从$ _GET ['other_params']获得“12” ; 从php生成这样的链接: $ id = 0; //假设这是你的编号 $ title = $ _SESSION ['title']; $ link ='http://www.newstoday.com/readnews/'。 $ id。 '/'。 $称号; –

1

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

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

的$表示URL的末尾,但你想要的ID后的内容,你应该将其删除

0

首先,你需要确保PHP是能够区分这两个网址之间。

RewriteRule ^readnews/([0-9]+)(\/[a-z-]+)?$ readnews.php?news_art_id=$1&title=$2 

这将使$_POST['title']包含news-of-the-day如果标题出现在URL - 否则$_POST['title']将是空的。在此之后,您可以简单地检查它是否由PHP设置,如果不是则重定向:

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

你的例子......有没有像ths这样的锚链接是正确的?..... News healine

+0

是的,'/ readnews/12'和'/ readnews/12/title'应该可以工作。前者应该重定向到后者。 – kba