...

View Full Version : .htaccess - subdirectories -> variables and trailing slash



wildreason
08-06-2010, 05:41 PM
My /browse script does all the heavy lifting for handling categories (and pagination), and recipes. Recipes don't use any type of pagination, but categories do.

/browse/recipe/this-is-a-recipe becomes
/browse/index.php?recipe=this-is-a-recipe

/browse/category/All becomes
/browse/index.php?cat=All

/browse/category/All/page/2/ becomes
/browse/index.php?cat=All&page=2

Using the following rewrite, this WORKS



RewriteEngine on
RewriteRule ^recipe/(.*)$ index.php?recipe=$1
RewriteRule ^category/([^/]+)/page/([^/]+) index.php?cat=$1&page=$2
RewriteRule ^category/(.*)$ index.php?cat=$1


However... If I do

/browse/category/All/ or
/browse/category/page/2

It fails. The trailing slash is giving me a problem and I'm not sure how to write all conditions to ignore the trailing slash. I also don't want two URL's so I'd like /browse/category/All to be rewritten as /browse/category/All/.

Any suggestions?

http://www.whatstoeat.co/browse/

Eldarrion
08-18-2010, 10:03 PM
There is a relatively easy solution. Actually, there are two, but I will address the one that doesn't require you to add about 3-4 lines to your redirects.

Add a trailing slash at the end of each of your matching rules and make it optional (With a following question mark) like so:



RewriteEngine on
RewriteRule ^recipe/(.*)/?$ index.php?recipe=$1
RewriteRule ^category/([^/]+)/page/([^/]+)/?$ index.php?cat=$1&page=$2
RewriteRule ^category/(.*)/?$ index.php?cat=$1


Enjoy.

wildreason
08-25-2010, 05:00 AM
Thanks... so a ? after the character indicates it is optional?

jphilipson
08-26-2010, 08:11 AM
Thanks... so a ? after the character indicates it is optional?

That is correct, a question mark after a character makes it optional



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum