Today I needed to do a 301 redirect for an old category page on a client's site to a new category which contained spaces in the filename. The solution to this issue seemed like it would be easy and straight forward, and maybe it is to some, but I found it to be tricky as I had never escaped a space in an Apache RewriteRule on the destination page.
The rewrite rule needed to rewrite:
/scan/mp=cat/se=Video Games
to:
/scan/mp=cat/se=DS Video Games
I was able to get the first part of the rewrite rule quickly:
^/scan/mp=cat/se=Video\sGames\.html$
The issue was figuring out how to properly escape the space on the destination page. A literal space, %20 and \s all failed to work properly. Jon Jensen took a look and suggested a standard unix escape of '\ ' and that worked. Some times a solution is right under your nose and it's obvious once you step back or ask for help from another engineer. Googling for the issue did not turn up such a simple solution, thus the reason for this blog posting.
The final rule:
RewriteRule ^/scan/mp=cat/se=Video\sGames\.html$ http://www.site.com/scan/mp=cat/se=DS\ Video\ Games.html [L,R=301]

5 comments:
Nice..
It Works. %2x or %3x (fill in x) doesn't work in apache rewrite. I think this is because %2 and %3 are also apache rewrite varibles.
using / (with space!) solved it!
Thanks for the tip!
Thank you for posting! I guess that this would have never crossed my mind (escaping a space I mean). Thanks again!
Thanks for posting! A great help!
Thanks for this. Not the most intuitive of methods and had us stumped for ages!
Post a Comment