tags:

views:

15

answers:

1

I have real trouble understanding mod_rewrite, and got some help in creating a RewriteRule to have simple urls for our site

RewriteRule ^news/([a-zA-Z0-9_-]+)$ /news/$1/ [R]
RewriteRule ^news/([a-zA-Z0-9_-]+)/$ news/show.php?id=$1

So someone can visit site.com/news/700/ and the url will look the same to them, but puts the request to our server with /news/show.php?=700 to pull the article out.

I want to include the title of the article into the url, just to make it more user friendly when someone see's a link to our site.

site.com/news/700/heres-our-important-title

Can someone let me know what I need to change to make this happen so the code will still work properly? So a url can have that extra text at the end, but it will still only pull the id number after /news/.

Thanks.

+1  A: 

You just need to update your second RewriteRule to allow (optionally) for the title at the end. Also, as a bit of house keeping, the first rule needs to be modified for an unrelated reason:

RewriteRule ^news/([a-zA-Z0-9_-]+)$    /news/$1/ [R=301,L]
RewriteRule ^news/([a-zA-Z0-9_-]+)/.*$ news/show.php?id=$1

It might also not be a terrible idea to change your character class [a-zA-Z0-9_-] to just [0-9] if your IDs are only numeric, depending on how you like to handle 404s.

Tim Stone
Worked perfectly, thank you for the help.
scatteredbomb