News

Welcome to End Point’s blog

Ongoing observations by End Point people

Apache RewriteRule to a destination URL containing a space

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]

6 comments:

Robert said...

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!

Valery Buchinsky said...
This comment has been removed by the author.
Valery Buchinsky said...

Thank you for posting! I guess that this would have never crossed my mind (escaping a space I mean). Thanks again!

S. Hill said...

Thanks for posting! A great help!

Nick said...

Thanks for this. Not the most intuitive of methods and had us stumped for ages!

Sandip said...

Nice post.. I have similar problem but with # character. My destination URL's contains "#" [hash] character. Could you please help me out to solve this issue. I would really appreciate you help..