Rewrite for all urls

I would like to rewrite something like:

http//www.example.com/index.php?var1=val1&var2=val2&var3=val3

IN

http://www.example.com/var1/val1/var2/val2/var3/val3/

I am looking for a solution to work with any number of variables. It can be done?

+3
source share
2 answers

Take a look at this question: Can mod_rewrite convert any number of parameters with any names?

My answer can be used in your case too:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', trim($_SERVER['REQUEST_URI_PATH'], '/'));
for ($i=0, $n=count($segments); $i<$n; $i+=2) {
    $_GET[rawurldecode($segments[$i])] = ($i+1 < $length) ? rawurldecode($segments[$i+1]) : null;
}

And the corresponding rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

This rule overwrites any request that cannot be mapped to an existing file or directory, c index.php.

+4
source

. , RewriteRule, ( ) , "", , .

, :

RewriteRule ^/index.php - [chain]
RewriteRule ^(.*)[=&](.*)$ $1/$2 [next]
RewriteRule ^/index.php\? "/"

, , , "/index.php". "chain" , , . an = / /, , , . , , , "/index.php?". .

0

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


All Articles