Alex Rivera | Logout

mod_rewrite with spaces in the urls

Asked 2009-01-04T10:42:39.397
37

I need to set up some RewriteRules to redirect a URL which has a space in it. I've tried this:

RewriteRule ^article/with%20spaces.html$ /article/without_spaces.html [R=301,L]

... but it doesn't work. Putting in a space instead of %20 causes a 500 Internal Server Error. How do I add a space?

Edit
Report

2 Answers

15

If you want to avoid the complexity of escaping each space (e.g. if you plan to have this file automatically generated), you can simply use quotes:

RewriteRule "^article/with spaces.html$" /article/without_spaces.html [R=301,L]

Furthermore, these quotes can be used to encase any one expected argument:

RewriteRule "^article/with spaces.html$" "/article/without_spaces.html" [R=301,L]
answered 2013-01-14T16:15:07.503
1

Ah, I've found a solution: use the regex style to show a space:

RewriteRule ^article/with\sspaces.html$ ...

Though, I suspect that this would match all the other whitespace characters too (tabs, etc), but I don't think it's going to be much of a problem.

answered 2009-01-04T10:45:07.550

Your Answer