Mod Rewrite Using Urlencoded & Aka Ampersand

liunx

Guest
I have some issues with modrewrite and the ampersand. <br /><br />I encoded the ampersand to %26, but it doesn't seem to work. View the following php scripts to replicate the issue. <br /><br /><b>Using Simple Get Request</b><br /><a href="http://section31.us/temp/modrewrite/get.php?foo=a%26b" target="_blank">http://section31.us/temp/modrewrite/get.php?foo=a%26b</a><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->prints Array ( [foo] => a&b )<!--c2--></div><!--ec2--><br /><br /><b>Using Mod Rewrite</b> (RewriteRule ^foo/(.*) get.php?foo=$1)<br /><a href="http://section31.us/temp/modrewrite/foo/a%26b" target="_blank">http://section31.us/temp/modrewrite/foo/a%26b</a><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->prints Array ( [foo] => a => )<!--c2--></div><!--ec2--><br /><br />Anyone have any experience using an ampersand with mod rewrite.<!--content-->
From what I've been able to find on the internet, this is a known issue with mod_rewrite that is currently not fixed.<br /><br />Depending on your script, you may be able to work around the issue with one of these options:<br /><br />1) Double-URL-encode ampersands<br /><br />As you noted above, ampersands ('&') can be encoded in a URL as '%26'. Encode the '%26' as well, with '%' encoded as '%25'. This would result in the ampersand being replaced with '%2526'. Since mod_rewrite only perform one pass in decoding URLs, the string '%2526' will be decoded into '%26', which will then be passed correctly to your script.<br /><br />2) Change the query string argument separator in PHP<br /><br />In URL query strings, parameters can be separated with '&' or ';'. By default, PHP uses '&' to separate multiple parameters in a URL's query string. You can configure PHP to use ';' instead by adding the following to your .htaccess file:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->php_value arg_separator.input ";"<!--c2--></div><!--ec2--><br /><br />3) Parse the query string yourself<br /><br />The query string that was submitted in the URL to your script will be present in the PHP global variable $_SERVER['QUERY_STRING']. Your script could read the query string directly and parse it into variables in however manner you see fit.<br /><br />Hope this helps...<!--content-->
Thanks David. Your first method will do nicely. Thanks.<!--content-->
 
Back
Top