views:

39

answers:

1

Using mod rewrite for the first time. Please help me with these rules

I'd like my urls rewritten for pages as follows:

list.php?city=dallas >>> list/city/dallas

profile.php?id=12 >>> profile/zaknuman (username retrieved from db)

story.php?id=33 >>> story/there-are-no-ants-in-texas (story title retrieved from db)

+3  A: 
RewriteRule list/city/([a-zA-Z])$ list.php?city=$1

which should match every character in the range a-z and A-Z after the final slash.

The other two I believe you'll need to embed the slug ('zaknuman' and 'there-are-no-ants-in-texas') in the database and then you'll be able to retrieve that slug from the database and get your ID that way, vis:

RewriteRule profile/(.*)$ profile.php?slug=$1

RewriteRule story/(.*)$ story.php?slug=$1

These last 2 match every character after the final slash.

EDIT: Don't forget to make sure you have "RewriteEngine On" in your .htaccess file or Apache configuration!

richsage
Don’t forget to mark the start of the string.
Gumbo
@Gumbo - yes, I left the start off deliberately since the OP hadn't mentioned the URL path they was using. Good point though - @gAMBOOKa, prepend a ^ to the start of the rule to match the start of the string.
richsage