• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mod_rewrite and plus signs in source URL

 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18301
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18301
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic