Score:0

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

cn flag

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.

Score:0
cn flag

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:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.