0

Is it possible to access a modified query string in a RewriteCond after applying a RewriteRule?

What I'm trying is to apply ...

RewriteCond %{QUERY_STRING} ^(.*)&?id=([^&]+)&?(.*)$ [NC]
RewriteRule ^/app\.jsp$ /app/%2?%1%3 [R=302]

... to rewrite following urls into "nice" ones. That already works, but for (2) and (3) there are still unneccessary trailing "?" and "%" chars that I'd like to remove:

/app.jsp?id=123&b=1 -> /app/123?b=1    (1)
/app.jsp?id=123     -> /app/123?       (2)
/app.jsp?b=1&id=123 -> /app/123?b=1&   (3)

My idea is to add additional conditional RewriteRule's to remove the trailing "?" and "%" chars from the modified query string but I don't know how to access the latter within subsequent RewriteCond's. Thank you for any hints.

1 Answer 1

1

Ok, I found a simple solution myself:

RewriteCond   %{QUERY_STRING}       ^()id=([^&]+)(.*)$  [NC,OR]
RewriteCond \?%{QUERY_STRING}  ^(\?.*)&id=([^&]+)(.*)$  [NC]
RewriteRule ^/app\.jsp$ /app/%2%1%3                     [R=302,QSD,L]

Rewrite results:

/app.jsp?id=123&b=1      -> /app/123?b=1       (1)
/app.jsp?id=123          -> /app/123           (2)
/app.jsp?b=1&id=123      -> /app/123?b=1       (3)
/app.jsp?b=1&id=123&d=2  -> /app/123?b=1&d=2   (4)

Some remarks:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .