2

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.

Lucas
  • 16,930
  • 31
  • 110
  • 182
  • That part is called "fragment" and you can get it in this way: $url=parse_url("http://domain.com/site/gallery/1#photo45 "); echo $url["fragment"]; //This variable contains the fragment use this example on http://stackoverflow.com/a/2317518/4817575 – Jatinder Kaur Sep 03 '16 at 07:21
  • @JatinderKaur I know, but I don't want to do that - I want the URL to be rewritten to `index.php?data=hello%23abc` – Lucas Sep 03 '16 at 07:22
  • use [L,R] to see what URL is really rewritten to, `abc` will probably be there – Dusan Bajic Sep 03 '16 at 07:46

1 Answers1

3

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

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105