aspose file tools
The moose likes Tomcat and the fly likes mod_rewrite and plus signs in source URL Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Products » Tomcat
Reply 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
Greenhorn

Joined: Sep 24, 2012
Posts: 26
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: 14475
    
    7

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
Greenhorn

Joined: Sep 24, 2012
Posts: 26
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: 14475
    
    7

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
Greenhorn

Joined: Sep 24, 2012
Posts: 26
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: mod_rewrite and plus signs in source URL
 
Similar Threads
straing servlet URL
Servlet mapping
Path to servlet
The requested resource is not available
sql error when connecting to mysql error