Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to pass my original URL as a query string for a servlet controller using a TARGET_URL parameter. Is it possible to do a URLencode for the TARGET_URL?

Here is an example: Orginal: would become:

share|improve this question

migrated from Feb 10 '11 at 20:15

This question came from our site for professional and enthusiast programmers.

Yes, it is possible using mod_rewrite and an 'escape' rewrite map. See

First, you need to define the rewrite map in your server or virtual host config, like this:

RewriteMap escaped int:escape

This creates a rewrite map named "escaped" that will translate special chars in the string to their hex encodings. You can use it like this ${escaped:text to escape}.

Next, you need to work up an appropriate set of rewrite rules to pass the information to your handler:

RewriteRule .* - [E=HT:http]
RewriteCond %{HTTPS} =on
RewriteRule .* -  [E=HT:https]
RewriteRule .* %{ENV:HT}://${escaped:%{ENV:HT}://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING}}

The first three lines figure out whether you should use 'http' or 'https' in your final rewrite. The last line makes a go at putting the original URL through the escape rewrite map and sending it along to your servlet/handler.

This is a little complicated, since it tries to handle http and https. One problem is that it will always include the ? char, even if the original request does not have a query string.

The last comment I have would be to suggest that you think about just proxying or using AJP to your servlet controller, which would make all this URL parameter manipulation unnecessary.

share|improve this answer

You could do this with the help of an external program. mod_rewrite supports this with the rewrite_map functionality. But it has performance warnings all over.

Can you explain a little further why you would like to do this in the first place? Why can't you just leave it as-is?

share|improve this answer
I'm trying to do exactly what this guy is mentioning there: link I need to forward the original request to a servlet controller to make additional processing before sending back to original request URL – ctesti Feb 10 '11 at 23:20

Maybe something like:

RewriteEngine On
RewriteRule ^/(app1/.*)$1
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.