Apache RewriteMap with URLs containing space doesn't work

You can use a second rewrite map, the internal function 'escape' this turns spaces into %20:

RewriteMap ec int:escape

RewriteMap redirects dbm=db:/data/apps/project/current/configuration/etc/httpd/conf/redirects.db

RewriteCond ${redirects:${ec:$1}} !=""

RewriteRule ^(.*)$ ${redirects:${ec:$1}} [redirect=permanent,last]

Then in your own rewrite map db you can have:

/Universités-direct%20/

This should then match.


You can solve this by extracting the encoded URI from the %{THE_REQUEST} variable and using that to do the lookup. You need to put the encoded URIs in the map though of course. Something like the following:

RewriteEngine On
RewriteMap redirects dbm=db:/data/apps/project/current/configuration/etc/httpd/conf/redirects.db
RewriteCond %{THE_REQUEST} "\w+ ([^ ]+)"
RewriteRule ^ - [E=MYVAR:%1]

RewriteCond ${redirects:%{ENV:MYVAR}} !=""
RewriteRule ^ ${redirects:%{ENV:MYVAR}} [redirect=permanent,last] [B]

I've only tested it with a text based map instead of the DB one though. This will probably need modification if you have to deal with URLs with query strings.