1

I'm moving over from IIS to Apache (on Windows) and struggling with adapting a rewrite rule (using Helicon ISAPI_Rewrite 3 in IIS).

The rule maps what looks like a directory structure path back into a set of query string parameters. There could be any number of parameters in the path.

E.g.

/basket/param1/value1/param2/value2/param3/value3 ...and so on...

Becomes...

/basket?param1=value1&param2=value2&param3=value3 ...and so on...

Rule in ISAPI_Rewrite:

#  This rule simply reverts parameters that appear as folders back to standard parameters
#  e.g. /search-results/search-value/red/results/10 becomes /search-results?search-value=red&results=10

RewriteRule ^/(.*?)/([^/]*)/([^/]*)(/.+)? /$1$4?$2=$3  [NC,LP,QSA]

I first spotted that Apache doesn't have the 'LP' flag, so swapped it for the N=10 as a test for looping...

RewriteRule ^(.*?)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,N=10,QSA]

However the Apache error logs show the same parameters being added over and over again until the number of loops on the N flag is reached, ending in a HTTP 500 error.

Any ideas where I'm going wrong?!?

1 Answers1

0

After having done much head scratching and engaging my Google Foo I have located the solution to all my problems on another Stack Overflow comment...

https://stackoverflow.com/a/5520004/14054970

Essentially...

apparently there's been an issue with mod_rewrite re-appending post-fix part in certain cases https://issues.apache.org/bugzilla/show_bug.cgi?id=38642

The problem:

If multiple RewriteRules within a .htaccess file match, unwanted copies of PATH_INFO may accumulate at the end of the URI.

If you are on Apache 2.2.12 or later, you can use the DPI flag to prevent this http://httpd.apache.org/docs/2.2/rewrite/flags.html

I'm using Apache 2.4, so my Rewrite rule now looks as follows (and I'll be adding the DPI flag to all rules to be safe)...

RewriteRule ^(.*?)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3  [NC,N=1000,QSA,DPI]