think123 think123 - 3 months ago 17
PHP Question

How to escape hash (#) characters in .htaccess?

I have something like the following in

.htaccess
:

RewriteRule ^a/(.+)$ index.php?data=$1 [L]


Simple enough, and works for most cases, except when I use the following URL:

http://example.com/a/hello%23abc


I expect this to set the
data
GET variable to
hello#abc
, but instead it breaks. I assume that it breaks because Apache "unescapes" the characters, making the url the following:

index.php?data=hello#abc


Which is probably why it's setting the
data
GET variable to
hello
.

Is there any way I can fix this?

Thanks.

Answer

Using the [B] flag should help in your case(available in Apache 2.2)

The [B] flag instructs RewriteRule to escape non-alphanumeric characters before applying the transformation.

RewriteRule ^a/(.+)$ index.php?data=$1 [L,B]

http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_b