You are almost doing it right. The only slight mistake is that urlencode
will prefer to use a plus to signify a space, which is only appropriate in query parameters and not path part. rawurlencode
would be better here.
However, that's not the reason it doesn't work. You are already generating the path /controller/action/par1/%2F
, which is correct.
It doesn't work in practice for you because Apache is trying to protect you (in a rather ineffective way) from directory traversal problems. If you include a %2F
sequence in a URL path, Apache will by default jump in and show your its own 404 page (ignoring any errordocument settings). To turn this feature (IMO: misfeature) off, you can use the AllowEncodedSlashes configuration directive.
Whether you can actually retrieve the encoded slash when it comes back to your script is another can of worms. Due to the bad design of the PATH_INFO
variable in the original CGI spec, many environments won't be able to see the %2F
as being anything different from an unencoded /
, potentially breaking routing you're doing from path parts.
It is usually best to avoid having slashes in path parts, because Apache is not the only server or language that will get confused by them. In general you should use query parameters for input where you need to be able to accept any string of bytes or characters, because path parts have practical problems in many servers with (a) /
, (b) \
, (c) the NUL character and (d) empty strings.