Would there be any way to continuously repeat execution of a single RewriteRule in .htaccess?
For example:
RewriteEngine On
# ...
RewriteCond %{QUERY_STRING} (.*?)(?:^|&)q=[^&]*(.*)
RewriteRule (.*) $1?%1%2
# ...
This rule will only be executed once before moving on to other rules. However, it could certainly match more than once:
Input: /?a=a&q=q&b=b&q=q&c=c
Expected: /?a=a&b=b&c=c
Actual: /?a=a&b=b&q=q&c=c
The closest way to do this seems to be the [N]
flag, but this only works if the rule is at the very top of the file. Unfortunately, this is not feasible if the rule relies on other rules being executed beforehand.
An answer to a similar question suggests that it is not possible, but it never addresses the question directly.
To add more context, the end goal is to essentially encode a query string:
RewriteEngine On
# ...
RewriteCond %{REQUEST_URI} ^/folder/(.+)
RewriteRule .? - [E=QUERY:%1,S=1]
RewriteRule .? - [S=2]
# Repeat the next rule as many times as possible
RewriteCond %{ENV:QUERY} (.*?)&(.*)
RewriteRule .? - [E=QUERY:%1\%26%2]
RewriteRule .? /folder?query=%{ENV:QUERY}
# ...
This would result in the following:
Input: /folder/a&b&c
Expected: /folder?query=a%26b%26c
Actual: /folder?query=a%26b&c