Archive for May, 2010

Recovering HTTP $_GET variables when using Apache’s mod_rewrite

Thursday, May 13th, 2010

Anyone using Apache’s mod_rewrite to handle HTTP requests will know that your $_GET variables  are no longer recognised by the server as the URI is treated as a single string value.

PHP has a function, called mb_parse_str(), that parses the query string representation of an array into a multi-dimensional array. This can be used to correctly represent the query string part of the URL.

The below function returns an array that holds the same values you’d expect from $_GET.

function get_http_vars(){
  $query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
  mb_parse_str($query, $get);
  return $get;
}

To use the function simply follow the example below;

$get = get_http_vars();

© 2006 - 2010 Matt Lowden. Creative Commons all rights reserved.