RewriteRule ok with 3 parameters but I'm stuck with 2 parameters variant

Hi
I’m trying to get my URL cleans. Well, most of my php processed-files are ok. And I’m close to the goal … But…

As the RewriteRule works perfectly with 3 parameters (I pass the id ($1),then the category ($2), then finally the item($3), this simply doesn’t work when passing only the id($1) and the category($2).
Below is an extracted sample from my .htaccess file

As far as I can understand it : the “category” parameter don’t look processed (as I construct a SQL query to check the number of the selected cateogry nothing is returned…but still : it does works when a third parameter is added…)

Why would it be so ?


RewriteRule ^fr/([^/]*)/([^/]*)/([^/]*)/$ /project/?id=$1&cat=$2&item=$3&lang=fr [NC,L]
RewriteRule ^fr/([^/]*)/([^/]*)/$ /project/?id=$1&cat=$2&lang=fr [NC,L]

thanks for any efficient answer/hint :slight_smile:

K

okay I made it using the following trick :
if anyone interested :

$urlVars = explode(“/”,$_SERVER[‘REQUEST_URI’]);
$cat=$urlVars[4];
//will return my int.

Now my $cat can be retrieved from wherever I want to.

Something is off here

You’re trying to match an URL like /fr/id/cat/ right?
When you explode that on slashes you get an array like

  1. ‘’
  2. ‘fr’
  3. id
  4. cat
  5. ‘’

Yet you use [4] for the cat and that seems to work. What gives? :slight_smile:
Are you sure the URLs are okay and don’t contain double slashes for example (like /fr/id//cat/) ?

Also, in your rules it’s better to use + instead of * as * matches 0 or more, so you can leave an element empty, while + means 1 or more, so there has to be something there or else it won’t match.

Even better would be to use character classes. For example I’m guessing the id is always numeric, in which case (\d+) would be better than the generic ([^/]+), since the rule will then only match if the value is numeric so people can’t try to trick your application by providing alpha characters as ids for example.

Hi
I actually mistype the array (b’cause of the “prefix”), but based on what I retrieve it looks ok (for exampel I reach the 4th itm in the array which corresponds to the given category :

Also thanks for the hints for the RewriteRules, I will try to take the time to tests all these code tricks.
Best


array(6) {
  [0]=>
  string(0) ""
  [1]=>
  string(6) "prefix"
  [2]=>
  string(2) "fr"
  [3]=>
  string(3) "cat"
  [4]=>
  string(1) "0"
  [5]=>
  string(0) ""
}