2

I have the following line of code in a .htaccess file:

RewriteRule ^mypage/(.+)/$ mypage.php?a=$1

This rewriterule works most of the time. However, if I try passing a parameter like C++, which would be mypage/C%2B%2B/ then it does not work properly. It should go to mypage.php?a=C%2B%2B... but instead it seems to look like mypage.php?a=c++ and so the the PHP code sees the value of a=c<space><space>

How can I update the RewriteRule to pass the hex coding to the rewritten url ?

Thank you in advance

anubhava
  • 761,203
  • 64
  • 569
  • 643
Ben914
  • 51
  • 1
  • 5

2 Answers2

1

Trick is to use B flag. As per the manual:

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

Read more about it here in doc.

Your modified rule should look like this:

RewriteRule ^mypage/(.+?)/?$ mypage.php?a=$1 [L,QSA,NC,B]
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

After much searching, I found the answer inside another StackOverflow post: How to encode special characters using mod_rewrite & Apache?

Thanks for the help.

Community
  • 1
  • 1
Ben914
  • 51
  • 1
  • 5