Issues with multiple rewrite rules

Discuss practical ways rearrange URLs using mod_rewrite.

Issues with multiple rewrite rules

Postby umpljazz » Thu Aug 07, 2008 1:49 am

Hi,

I'm having a bit of trouble getting multiple rewrite rules to work as I planned for a recipe site I'm writing.

Basicially I have 2 absolute pages...

domain.com/category.php?id=chicken
domain.com/recipe.php?id=a-chicken-recipe

The respective friendly URL's I wish to use are along the lines of...

domain.com/chicken
domain.com/chicken/a-chicken-recipe/

Currently my rewrite code is as follows...

Code: Select all
RewriteEngine on

###add trailing slashes to http requests###
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) http://%{HTTP_HOST}/$1/ [R=301,L]

RewriteRule ^(.*)/$ /category.php?id=$1
RewriteRule ^(.*)/(.*)/$ /recipe.php?id=$2


But I think what is happening here is the first rule is catching both instances?

Any help would be appreciated.
Thanks
umpljazz
 
Posts: 2
Joined: Thu Aug 07, 2008 1:45 am

Postby umpljazz » Thu Aug 07, 2008 2:35 am

Ok I have had another play around and found that the following seems to work...

Code: Select all
RewriteRule ^(.*)/(.*)/$ /recipe.php?id=$2
RewriteRule ^([A-Za-z0-9\-]*)/$ /category.php?id=$1


I believe its because the previous one allowed for addional '/' in the URL (.*). The solution above isn't ideal however as it'll only allow for alphanumeric and '-' characters.

Anyone have any neater solutions?
umpljazz
 
Posts: 2
Joined: Thu Aug 07, 2008 1:45 am

Postby richardk » Thu Aug 07, 2008 12:55 pm

[^/] means anything but a /.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteRule ^([^/]+)/?$       /category.php?id=$1 [QSA,L]
RewriteRule ^[^/]+/([^/]+)/?$ /recipe.php?id=$1   [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 36 guests

cron