Apache Mod Rewrite and Escaped Hashes (and other characters)

If you are having issues with mod rewrite apparently abandoning sections of variables after encoded hashes then this could be your solution.

The use case is particularly clear when using mod_rewrite in front of a search engine (such as Solr which I am really enjoying working with at the moment).

Imagine someone search for a partcode ABC#123

This gets encoded to search/abc%23123

Your rewritten search term will then be mangled by Apache and your search script will only actually see ABC. That is of course a problem and the solution is not really clear.

After a bit of searching around I found the solution is to add a B flag to your mod_rewrite rule so that mod_rewrite will escape these characters so that they are cleanly passed through.

For example:

RewriteRule ^(.*)search/(.*)$ advanced_search_result.php?keywords=$2 [L,B]