1

I have a url that parts of it are converted into variables through htaccess and other parts are variables sent in GET.

mydomain.com/page/5/string?v2=foo&v3=bar

.httaccess sends 5 as a variable, let's call it v1

RewriteRule ^([^/]*)/([0-9]+)/(.*)$ page.php?v1=$2 [L,QSA]

And the rest of the query string are received as GET.

?v2=foo&v3=bar

In this example, it is very straight forward, but in the actual system the variables parsed by .htaccess are more than one and are dynamic,

I need to find a way to only get the variables sent in GET ignoring the ones from .htaccess.

I tried $_SERVER['QUERY_STRING'] but that one returns also the variables parsed by .htaccess:

echo $_SERVER['QUERY_STRING'] //returns v1=5&v2=foo&v3=bar

I only want to get v2=foo&v3=bar

anubhava
  • 761,203
  • 64
  • 569
  • 643
multimediaxp
  • 9,348
  • 13
  • 49
  • 80
  • You could probably start with [strlok](http://php.net/manual/en/function.strtok.php) or [explode](http://php.net/manual/en/function.explode.php) on `$_SERVER['REQUEST_URI']` using `?` as the character to search for... – CD001 Sep 06 '17 at 15:54
  • Thanks, your link to strlok doesn't work. – multimediaxp Sep 06 '17 at 15:55
  • I got my `()` and `[]` mixed up in the comment and garbaged it while editing it - fixed now ;) – CD001 Sep 06 '17 at 15:56
  • ... forgot `$_SERVER['REQUEST_URI']` doesn't contain the querystring - so exploding on `?` won't work anyway. – CD001 Sep 06 '17 at 16:02

3 Answers3

2

I think you can read $_SERVER['REQUEST_URI'] and use proper regex to take the GET variables from URL. And then (if you need) you could write function to split them to array of variables:

$get = array(); //array for GET values
$uri = $_SERVER['REQUEST_URI'];

$uri_parts = explode('?', $uri, 2);
$get_string = $uri_parts[1]; //[0] is address

$get_pairs = explode('&', $get_string); //split to key=value pairs
foreach($get_pairs as $get_pair){
  list($key, $value) = explode('=', $get_pair, 2); //split key and value
  $get[$key] = $value;
}

$get_string contains string like a=bc&d=ef

$get contains key-value pairs (like $_GET)

Marcin Szwarc
  • 569
  • 3
  • 13
1

I need to find a way to only get the variables sent in GET ignoring the ones from .htaccess.

You can use an environment variable to pass original query string to your php file:

RewriteRule ^([^/]+)/([0-9]+)/(.*)$ page.php?v1=$2 [E=qs:%{QUERY_STRING},L,QSA]

Now access original query string using this variable in php code:

$_SERVER["REDIRECT_qs"]

This should print:

v2=foo&v3=bar

Whereas $_SERVER["QUERY_STRING"] will give you full query string i.e. v1=5&v2=foo&v3=bar

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks! This is very good, but I am looking for a PHP solution. I am not sure if I will be allowed to touch the htaccess. – multimediaxp Sep 06 '17 at 16:48
  • Well ti doesn't change the rule behavior at all. It just passes an additional env variable to php code and bonus is that no additional coding needed in php. – anubhava Sep 06 '17 at 16:57
0

how can I define variable in .htaccess file and use it?

Example

SetEnv SPECIAL_PATH /foo/bin

http://httpd.apache.org/docs/current/mod/mod_env.html#setenv

  • Didn't think you could set dynamic environment variables in htaccess? If you try to set it to ${QUERY_STRING}, it'll set it to be the literal string rather than the value – James Hunt Sep 06 '17 at 16:06