Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I need to perform an equivalent rewrite to:

RewriteRule ^/this%20file.pdf http://www.example.com/

How do I handle the %20?

I have tried

RewriteRule ^/this\%20file.pdf http://www.example.com/

and

RewriteRule ^/this(\s|%20)file.pdf http://www.example.com/

but these have not worked.

The website is part of a Wordpress network, so it doesn't have it's own directory or subdomain where I can place a .htaccess file containing redirects; it's a virtual subdomain with domain mapping. So, the Wordpress .htaccess rewriterule is preceeded by

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
share|improve this question
add comment

1 Answer

You shouldn't need to use percent encoding/hexcode in mod_rewrite parameters since mod_rewrite will encode special characters (?, #, , &, etc.) by default. To write a space in the rewrite pattern, use \s or just escape the space with a backslash (\). Whether space gets encoded as %20 or + depends on whether it's part of the query string or the URI.

So your rule should look like this:

RewriteRule ^/this\ file.pdf http://www.example.com/

Or:

RewriteRule ^/this\sfile.pdf http://www.example.com/

If you do want to manually specify the URI encoding, then you need to use the NE (noescape) flag to tell mod_rewrite not to escape special characters.

Otherwise, /this%20file.pdf gets treated as /this%2520file.pdf, as the % will be encoded as %25.

share|improve this answer
    
Thanks Lese. How do you use the NE (noescape) flag? –  Steve Dec 31 '12 at 1:07
    
If the solution is to use \s, why didn't (\s|%20) work? –  Steve Dec 31 '12 at 1:08
    
@Steve: the NE flag is just placed in the square brackets at the end like with the NC and other flags. You separate them with commas if you want to use more than one flag. As for your second question, (\s|%20) should have worked. The parentheses and %20 are unnecessary, but it doesn't get in the way either. I've tested it on my own server, and it does in fact work. Perhaps you've got something else interfering with the rewrite rule such as an incorrect RewriteBase directive or example.com needs to be www.example.com? –  Lèse majesté Dec 31 '12 at 1:45
    
@Steve: Does the rewrite rule work if you try it without a space in the pattern URL? Is there a previous rule using the C (chain) flag? If there are rules after it, you may want to use the L flag to ensure no other rules get applied after it. –  Lèse majesté Dec 31 '12 at 1:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.