wood burning stoves 2.0*
The moose likes Tomcat and the fly likes mod_rewrite and plus signs in source URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "mod_rewrite and plus signs in source URL" Watch "mod_rewrite and plus signs in source URL" New topic
Author

mod_rewrite and plus signs in source URL

Stan Lederer
Ranch Hand

Joined: Sep 24, 2012
Posts: 35
I've got a servlet and an Apache rewrite rule that looks like this:

This works great until I have a plus sign in my URL.

I want to have:

abc+def.html

rewritten as:

http://solrsandbox:8080/servlet/SolrSearcher?userParam=abc+def

How can I add that "+" to the list of acceptable characters in the source url?

Thanks very much.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

If you were to consult the RFCs that define URLs for the Internet, you'd discover that "+" has a specific meaning in a URL. So in order for the "+" to propagate as a text character and not as a URL parse marker, you need to "escape" it. URL escaped characters are in the form "%nn", where "nn" is the numeric code that corresponds to the escaped character.


Customer surveys are for companies who didn't pay proper attention to begin with.
Stan Lederer
Ranch Hand

Joined: Sep 24, 2012
Posts: 35
Tim,

Thanks for your response. What I still can't figure out is how to recognize a plus sign, or its encoded version, in a URL.

I've played with the rewrite rule and this will work for me (if I don't find a better solution):

This will allow all characters in the URL. Then, in my servlet I grab the Query String:

This has spaces where plus signs were, which is just fine since I'm not expecting spaces in the URL.

This is not the ideal approach because now the burden is on my servlet to make sure no nasty characters get into the input. But, failing a better solution, this will work.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15958
    
  19

That's exactly why you have to escape a plus sign to actually get a plus sign. The URL parser recognizes "+" as a space, since URLs cannot contain actual spaces, and the alternative "%20" isn't as compact.

Remember when coding rewrite rules that "+" also has meaning in regular expressions. So "+" isn't the best choice for coding into URLs.
Stan Lederer
Ranch Hand

Joined: Sep 24, 2012
Posts: 35
Actually, I discovered that I can have the "+" on the left side of the rewrite rule and it works. I had assumed that "+" needed escaping. I was wrong.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: mod_rewrite and plus signs in source URL
 
Similar Threads
straing servlet URL
The requested resource is not available
Path to servlet
sql error when connecting to mysql error
Servlet mapping