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/


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]

2 Answers 2


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/


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.

  • Thanks Lese. How do you use the NE (noescape) flag?
    – Steve
    Dec 31, 2012 at 1:07
  • If the solution is to use \s, why didn't (\s|%20) work?
    – Steve
    Dec 31, 2012 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? Dec 31, 2012 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. Dec 31, 2012 at 1:49
  • On Open Light Speed servers it may be necessary to put \ before file extension. RewriteRule ^/this\ is\ the\ file\.pdf example.com [R=301, L]
    – K H
    Dec 18, 2021 at 17:44
RewriteRule ^/this%20file.pdf http://www.example.com/

The URL-path matched by the RewriteRule pattern (regex) is already %-decoded. So, to match an encoded space (ie. %20) in the request you match against a literal space in the regex.

(Aside: If you try to match a literal %20 here then you are matching against a doubly-encoded space, ie. %2520, in the requested URL-path.)

As mentioned in @Lèsemajesté answer, you can do this in the regex itself by either backslash-escaping the space ie. \ , or using the \s shorthand character class to match any whitespace character.

OR, you can simply surround the argument in double-quotes and use a literal un-escaped space. For example:

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

The double-quotes are an Apache mechanism for when arguments contain spaces, since spaces are argument delimiters in Apache config files.

NB: Since we are dealing with .htaccess here you should also remove (or make optional - as I have done above) the slash prefix on the URL-path matched by the RewriteRule pattern. In a directory context (ie. .htaccess) the URL-path matched by the RewriteRule pattern does not start with a slash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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