Can mod_rewrite convert any number of parameters with any names?

I'm full n00b in mod_rewrite, and what I'm trying to do sounds simple:

instead of having domain.com/ script.php? a = 1 & b = 2 & c = 3 I would like:

domain.com/script | a: 1, b: 2; from: 3

The problem is that my script accepts a large number of parameters in many combinations, and the order is not important, so coding each of them in an expression and waiting for a certain order is not feasible. So can a rule be established that simply passes all the parameters to the script, regardless of the order or number of parameters? So if someone dials

domain.com/script | a: 1; b: 2; j: 7 it will pass all these parameters and values ​​in the same way as with domain.com/ script | b: 2; a: 1 ;?

Thanks!

0
source share
1 answer

I would use PHP to parse the requested URL:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$params = array();
foreach (explode(',', substr($_SERVER['REQUEST_URI_PATH'], 6)) as $param) {
    if (preg_match('/^([^:]+):?(.*)$/', $param, $match)) {
        $param[rawurldecode($match[1])] = rawurldecode($match[2]);
    }
}
var_dump($params);

And the mod_rewrite rule for overwriting such requests into your file /script.php:

RewriteRule ^script\|.+ script.php [L]
+3
source

Source: https://habr.com/ru/post/1708820/


All Articles