views:

42

answers:

2

Alright, I have my urls set up to rewrite "news_item.php?id=1" to "blog/1/Title-of-post".

This works great apart from when a title has numbers at the start for example "blog/23/12-days-to-go" it can't find the right id.

Here's how I have it setup, maybe someone can make sense of it.

Rewrite rules

RewriteRule ^blog/([0-9]+)/([a-z-]+)/$ ./news_item.php?id=$1&t=$2
RewriteRule blog/([[0-9]+)/([a-z-]+) news_item.php?id=$1&t=$2

Get ID from url

$id = intval($_GET['id']);

Then I just use

`WHERE `id`=$id`

as my query

Link used

/blog/$id/$title/

So it works fine apart from when there is a number at the start of the title, a number within the title is fine though. Can anyone suggest what to do?

+3  A: 

The problem with the ids is in your regular expression. You aren't looking for numbers. Use something like:

RewriteRule ^blog/([0-9]+)/([A-Za-z0-9-]+)/$ ./news_item.php?id=$1&t=$2

The "number in the title" works because you are only pulling the alpha characters up to the first number. (e.g. "abc123def" becomes "abc")

konforce
A: 

First of, in your example you got an extra [ in your second RewriteRule.

A RewriteRule like the following should work as you want it to:

RewriteRule ^blog/([0-9]+)/([^/]*) ./news_item.php?id=$1&t=$2

The following URI /blog/12/24-days-to-go/extra-stuff should give you the following in news_item.php:

$_GET['id'] = 12;
$_GET['t'] = '24-days-to-go';

Stripping everything after the third /.

Edit

An URI like /blog/12/ would work as well, sending 12 as the id and giving you an empty $_GET['t']

xintron
Thanks a lot :)
Nick