views:

28

answers:

1

I need help writing a simple regex for RewriteRule for mod_rewrite in htaccess. So, here is what I am trying to accomplish:

books/2010-the-world-by-hopkins-139_PPS-1234567

should go to

index.php?pagename=mypage&PPS=1234567&description=2010-the-world-by-hopkins-139

So, in pseducode, the regex has to split the part after books by _ and I should get it into two parts:

  1. PPS (it is always a numeric with 1+ variable number of digits). This is the part after _PPS-
  2. Description (it is always a string). This is the part containing ANYTHING before the _.

I guess the RewriteRule will be something like:

RewriteRule books/(.*)_(.*) index.php?pagename=mypage&PPS=$2&description=$1

But I need correct regex. Plese help.

+1  A: 

something like: but flip the $1 and $2 ;)

^books/(.*)_PPS-([0-9]{1,})$
Caspar Kleijne
Thanks, it worked fine.
WhatIsOpenID
please mind that you mix VALIDATION of the given url with REWRITING the url at the same time, especially in the second group of the Regex. Consider to separate it and always validate the querystrings again in the index.php.
Caspar Kleijne