开发者

Modify Mod_rewrite rule to add article title to URL

I have searched SO but all the examples here are not working for me.

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

$title = $_SESSION['title']; //local variable contains article title

Let's say the title of the article is news-of-the-day How can I modify my rule to

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

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

Rewrit开发者_如何学JAVAeEngine on
RewriteRule ^readnews/([0-9]+)$ readnews.php?news_art_id=$1


Try something like that:

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

Added later: everything what goes after id will be in other_params GET param.


Your regex is incorrect. it should be:

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

The $ indicates the end of the URL, but you want content after the id, you should remove it


First of all, you need to make sure PHP is able to distinguish between the two URLs.

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

This will make $_POST['title'] contain news-of-the-day if the title is present in the URL - otherwise $_POST['title'] will be empty. After this, you can simply check if it's set by PHP and redirect if not:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜