adding a second variable? simple right?

Discussions about Apache mod Rewrite, .htaccess, Use, experiences ... URL Rewriting.

Moderator: Moderators

adding a second variable? simple right?

Postby kred » Tue Aug 14, 2007 8:23 am

Hi all,
I have a product page on a site I'm working on, and I am using this rewrite rule
Code: Select all
RewriteRule ^\category/([A-Z][a-z](.*)+)$ /category.php?cat=$1 [L]

this gets the name of the category for the php script. THis works great and I'm not having any problems.
The problem is, that when there are more than 12 products on the page I want to break up the pages into 1,2,3 or how ever many other pages are. The way I am doing that is by adding "category?page=#" to the URL or like this "/catergory/page#".
Now my problem is that I don't know how to add that new variable page# to my rewrite rule. I tired something like this:
Code: Select all
RewriteRule ^\category/([A-Z][a-z](.*)+)/([0-9]+)$ /category.php?cat=$1&page=$2 [L]

but that didnt work.
What I need to do is pass both variables into the category.php page, but only when there are both var to be passed, - because not all the products have more than 1 page -

I hope that I have written my issue out clearly and would appreciate any help.
Thanks
K
kred
 
Posts: 3
Joined: Tue Aug 14, 2007 7:56 am

Advertisement

Postby SeO » Tue Aug 14, 2007 9:01 am

And welcome :D

First, I don't think you really need the backslash, and you can simplify a bit your RegEx :

Code: Select all
RewriteRule ^category/([a-z0-9_-]+)$ /category.php?cat=$1 [L,NC]


The [a-z0-9_-] means all a to z chars, plus numbers and "_" and "-", which is most likely what you are using in your titles. The NC flag stands for No Case, will thus match A-Z and a-z.
If you are using other chars, you may want to open it more :
Code: Select all
RewriteRule ^category/(.+)$ /category.php?cat=$1 [L,NC]

The dot means "any string", but it's generally faster to specify the chars to match.

Then, to add pagination, you must first decide your URL standard.

If you want to go for URLs like : /category/cat-title/pagexx, you shall use :
Code: Select all
RewriteRule ^category/([a-z0-9_-]+)(/page([0-9]+))?$ /category.php?cat=$1&page=$3 [L,NC]


This rewriterule will deal with both paginated and regular links, thanks ot the question mark "?". Note that this implies the page=$3, $2 being used to match the whole pagination string (/pagexx) where we only want the xx (thus the next var captured).
SeO
Admin
Admin
 
Posts: 6333
Joined: Wed Mar 15, 2006 9:41 pm

Postby kred » Tue Aug 14, 2007 10:21 am

Thanks for the quick reply.
I tried using your line but ran into some other complications.
1. I am using "." - the dot key in my URL's - ie. www.mysite.com/category/Judo.Brazilian_Jiu-Jitsu/2 - so I changed your line to:
Code: Select all
RewriteRule ^\category/(.+)(/([0-9]+))?$ /category.php?cat=$1&page=$3 [L,NC]

but that is giving me the whole string, ie. "Judo.Brazilian_Jiu-Jitsu/2" instead of just "Judo.Brazilian_Jiu-Jitsu" as $1 - - and that throws my SQL query out of whack.

Is there a way to stop $1 before the "/", or should I strip it in PHP?

Thanks again
K
kred
 
Posts: 3
Joined: Tue Aug 14, 2007 7:56 am

Postby SeO » Tue Aug 14, 2007 10:26 am

You can thus add the dot in the RegEx :

Code: Select all
RewriteRule ^category/([a-z0-9\._-]+)(/page([0-9]+))?$ /category.php?cat=$1&page=$3 [L,NC]


[a-z0-9\._-] will match a-zA-Z0-9 dots, underscores and hyphens ;)
SeO
Admin
Admin
 
Posts: 6333
Joined: Wed Mar 15, 2006 9:41 pm

Postby kred » Tue Aug 14, 2007 10:46 am

Thanks again,
I tried adding a "." there but didn't use the "\" so I got an error - I'm still new to this sport.
You have fixed my problem, I owe you a beer or whatever you would prefer to drink.

Thanks
K
kred
 
Posts: 3
Joined: Tue Aug 14, 2007 7:56 am

Postby SeO » Tue Aug 14, 2007 11:38 am

What about :
Code: Select all
RewriteRule ^category/([a-z0-9._-]+)(/page([0-9]+))?$ /category.php?cat=$1&page=$3 [L,NC]
SeO
Admin
Admin
 
Posts: 6333
Joined: Wed Mar 15, 2006 9:41 pm


Return to Apache mod Rewrite

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 4 guests