How to encode special characters using mod_rewrite & Apache?

前端 未结 5 1145
逝去的感伤
逝去的感伤 2020-11-29 07:38

I would like to have pretty URLs for my tagging system along with all the special characters: +, &, #, %, and =

5条回答
  •  [愿得一人]
    2020-11-29 07:57

    I meet the similar problem for mod_rewrite with + sign in url. The scenario like below:

    we have a url with + sign need rewrite like http://deskdomain/2013/08/09/a+b+c.html

    RewriteRule ^/(.*) http://mobiledomain/do/urlRedirect?url=http://%{HTTP_HOST}/$1

    The struts action urlRedirect get url parameter, do some change and using the url for another redirect. But in req.getParameter("url") the + sign change to empty, parameter url content is http://deskdomain/2013/08/09/a b c.html , that cause redirect 404 not found. For resolve it (get help from prior answer)we use rewrite flag B (escape backreferences), and NE (noescape)

    RewriteRule ^/(.*) http://mobiledomain/do/urlRedirect?url=http://%{HTTP_HOST}/$1 [B,NE]

    The B , will escape + to %2B , NE will prevent mod_write escape %2B to %252B (double escape + sign), so in req.getParameter("url")=http://deskdomain/2013/08/09/a+b+c.html

    I think the reason is req.getParameter("url") will do a unescape for us, the + sign can unescape to empty. You can try unescape %2B one time to + , then unescape + again to empty.

    "%2B" unescape-> "+" unescape-> " "

提交回复
热议问题