Can mod_rewrite convert any number of parameters with any names?

后端 未结 1 559
野趣味
野趣味 2021-01-07 15:20

I\'m a total n00b at 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 to have:

相关标签:
1条回答
  • 2021-01-07 15:35

    I would use PHP to parse the requested URL path:

    $_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 to rewrite such requests to your /script.php file:

    RewriteRule ^script\|.+ script.php [L]
    
    0 讨论(0)
提交回复
热议问题