Jump to content

Mod Rewrite Using Urlencoded & Aka Ampersand


Recommended Posts

I have some issues with modrewrite and the ampersand.


I encoded the ampersand to %26, but it doesn't seem to work. View the following php scripts to replicate the issue.


Using Simple Get Request


>prints Array ( [foo] => a&b )


Using Mod Rewrite (RewriteRule ^foo/(.*) get.php?foo=$1)


>prints Array ( [foo] => a [b] => )


Anyone have any experience using an ampersand with mod rewrite.

Link to comment
Share on other sites

From what I've been able to find on the internet, this is a known issue with mod_rewrite that is currently not fixed.


Depending on your script, you may be able to work around the issue with one of these options:


1) Double-URL-encode ampersands


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.


2) Change the query string argument separator in PHP


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:

>php_value arg_separator.input ";"


3) Parse the query string yourself


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.


Hope this helps...

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...