Rewrite plus sign to hex encoded

Topics: Developer Forum, User Forum
Dec 22, 2011 at 7:53 PM


I used to use a condition/rule like the following

RewriteCond %{REQUEST_FILENAME}     !-f
ReWriteRule ^([^+]*)\+(.*) $1%%20$2 [I]

to identify and replace a substituted "+" sign for a space.

Since the upgrade to 2.1 (or maybe it was into IIS7.5), that regex no longer works and it seems like there are 2 problems with it.

  1. The literal plus "\+" is not being recognized, and in fact causes IIRF to not fire at all for the request.
  2. The old "%%20" syntax is now, in fact, literal. So it inserts %%20 into the URL.

My new Rewrite rule looks like:

RewriteCond %{REQUEST_FILENAME}     !-f
RewriteRule ^([^+]*)\x2B(.*)$ $1%20$2 [I]

Where \x2B is the hex encoded "+" and I'm just substituting %20.

My question is two-fold.

Is this correct? I've put it through some minimal testing and it seems to work.

Is there a better way? As you can see, this would only work if the path contained a single + sign.

Thanks in advance.

PS Don't harp on me for not properly encoding the path on initial delivery, I didn't write the application. :)