Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite query string decrypt parameter

         

boulay

8:20 am on Jul 1, 2009 (gmt 0)

10+ Year Member



Hello,

I need to be able to rewrite a URL containing an "encrypted" parameter and to decrypt it.

The URL would be something like:

[mysite.com...]

knowing that:
- the order of the parameters in the query string could change
- the parameter name "id" could change;

So I have to identify the parameter between the tags XX

Then I would have to rewrite the URL to;

[mysite.com...]

I can write the regular expression do "decrypt" the parameter from 24234234 to 95999934 but Im unable to find it and replace it using query_string. Any ideas?

Thank you!

boulay

9:05 am on Jul 1, 2009 (gmt 0)

10+ Year Member



I did something like:

RewriteEngine on
rewritecond %{query_string} ^(.*&?)(\w+)=XX(.*)XX(.*)$
RewriteRule ^(.*)$ $1?%1%2=%3%4

which works. Could you let me know whether you think it's a good approach to solving my issue?

Thank you

jdMorgan

2:28 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For much better performance make your pattern as specific as possible, and simplify the back-reference:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*[a-z]+)=XX([0-9]+)XX(&.*)?$
RewriteRule ^(.*)$ $1?%1=%3%4

In general, avoid the use of multiple ".*" subpatterns in regular-expressions patterns. While they are "easy," they can also be horribly inefficient, because they force the matching engine to execute many (possibly thousands) of back-off-and-retry matching attempts. Using a more-specific subpattern often allows the match to be evaluated in a single left-to-right pass.

In this specific case, a few retries may be necessary, but the 'boundaries' between name/value pairs are still well-defined, and the number of retries will be much smaller.

Jim