Learning PCRE, please help

May 29, 2008 at 3:30 AM
Hi, I'm just starting to learn about Perl Compatible Regular Expressions... in fact I know nothing about programming in PERL at all.  So I hope someone could please help me.
I want to redirect my web address /news?xyz and /news/?xyz so it shows as /news/xyz/... (i guess I have that backwards... I want /news/xyz/ to always redirect to /news?xyz, where xyz could be anything)  I also want to set it up so /news/xyz redirects the user to /news/xyz/ with the backslash.

I have:
#redirects the old user references with the querystrings? to the new "hard paths"
RewriteRule    ^/News(?{1})(.*)$        /News/$2/   [I,R,L]

#rewrites for the server from /news/xyz/ to /news?xyz
RewriteRule    ^/News(/?)(.*)$          /News\?$2   [I,L]

it's not quite working as I want..  the 1st one leave the ? in, so it looks like /news/?xyz, instead of /news/xyz/ like I want, and the second one isn't stripping out the / at the end, if it's present. 

(note: I know that's not normal behaviour for querystrings... it's usually ?abc=xyz, but I made some server-side scripting a while ago that can deal with ?xyz to minimize the look of the address... now that I've discovered ISAPI rewriting, I'm trying to use that approach instead)
May 31, 2008 at 9:44 PM
There are some examples/tests in the download that show how to do this.  Specifically, check the TransformUrlSegmentsToQueryParamsIteratively example.

Or, you could look in the wiki for IIRF, the Map URL Segments to Query Params example. It's the same thing.

What I suggest you do is work up your rules the way you want em and then use the TestDriver.exe to run some URLs through your ruleset.
Your case is simpler than the general case handled in the example I cited above, because you just want ONE URL segment, whereas the example above maps ANY NUMBER of URL segments to query params.

good luck
May 31, 2008 at 9:50 PM
Specific to your Regex questions,

for the first one, try escaping the question mark with a backslash.  The ? is a significant character in regular expressions, so you need to write \? to capture it, rather than just ?,  Ok?  Otherwise it looks good. 

For the second one - same thing, escape the question mark.  I don't really get what you mean by "isn't stripping the / at the end".  Maybe an example of what you get, and what you expect to get, would be helpful.

When you do your tests, be sure to try unexpected input, to verify that the rules work well even in the face of bad URLs.   Standard testing recommendations I guess.