Welcome to WebmasterWorld Guest from 54.147.220.66

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite URLs with if statements?

more confusing regex than you can shake a stick at.

   
11:53 am on Apr 8, 2004 (gmt 0)

10+ Year Member



I'll try and be as general as I can here for simplicity's sake.

I have a file called file.php which can contain up to six variables in the query string (a=1&b=2&c=3 etc). I've rewritten the href URLs to the two most important variables, and then the four others in a query string, so a typical URL would look like host.com/a1/b2.htm while with the query string (less common occurances) it would be host.com/a1/b2.htm?c=3&d=4 etc.

I then have the following rewrite rule:

RewriteRule ^a([0-9]*)/b([0-9]*).htm (points to) file.php?a=$1&b=$2

which matches the two variables, but if the href URL has extra vars, they don't get included, because they don't redirect correctly to file.php

So is it possible to have a sort of 'if' statement in the rewrite rule, or will I need a new rule entirely? My thought is that if I can say: if there's a question mark after the .htm, then append its trailing contents to the file.php string in the form of &vars. I get the impression that something like (?(?[a-z]*)) would work, but I'm not very good with regular expression (if only they were human readable! :p)

Does this make sense?

Cheers,
WW.

3:48 pm on Apr 8, 2004 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



WW,

See the RewriteCond [httpd.apache.org] directive, and the [QSA] (query string append) flag of RuleRewriteRule [httpd.apache.org].

Jim

4:13 pm on Apr 8, 2004 (gmt 0)

10+ Year Member



I shall have a goosey gander, cheers. So much documentation for what basically comes down to a few characters :\
If it weren't for the perverse satisfaction of rewriting pretty URLs and condensing script, I'm sure I wouldn't bother :)
7:56 am on Apr 9, 2004 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Ah, but mod_rewrite and the regular expressions that it uses pack a lot of power into those few characters! :)

Jim