Apache mod_rewrite

2

I have a rewrite rule I found in a old question of the American OS:

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^/]+)/ $1.php?%1 [L]

This rule makes it possible for me to pass any number of variables through the URL without having to make a rule for each quantity:

 /mypage/param1/val1/param2/val2/param3/val3/...     --->
 /mypage.php?param1=val1&param2=val2&param3=val3&...

Notice that the first parameter passed turns the name of the php file, the  others go two by two and see the parameter and value of a variable  GET.

I want the first parameter to remain the file, but the second is the value of a variable that will always have the same name:

/test/foo/bar/baz/u/e/...     --->
/test.php?varfixa=foo&bar=baz&u=e&...

I use regex and could even do this if it were just regex, but .htacces has a few different things.

One way that can help me would also be to always send a index.php , but with two fixed variables:

/test/foo/bar/baz/u/e/...     --->
/index.php?varfixa1=test&varfixa2=foo&bar=baz&u=e&...
    
asked by anonymous 19.10.2016 / 19:24

1 answer

3
  

I want the first parameter to remain the file, but the second is the value of a variable that will always have the same name

RewriteEngine on

#Quando há um número ímpar de parâmetros 
#    (eliminar "?$2" se quiser ignorar a última variável vazia)
RewriteRule ^((?:(?:[^/]+/){2})+)([^/]+)/?$ $1?$2    [QSA,L]

#Vira as duas últimas variáveis no ?parâmetro=valor
RewriteRule ^([^/]+/.*/)([^/]+)/([^/]+)/?$ $1?$2=$3  [QSA,L]

#Vira as duas primeiras variáveis no arquivo+".php?varfixa="+valor
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?varfixa=$2    [QSA,L]

#Caso que há um só parâmetro
RewriteRule ^([^./]+)/*$ $1.php                      [QSA,L]

URL:

http://site.com.br/test/foo/bar/baz/u/e/v/f

Rewriting:

http://site.com.br/test.php?varfixa=foo&bar=baz&u=e&v=f


  

One way that can help me would also always be to index.php , but with two fixed variables

RewriteEngine on
RewriteRule ^((?:(?:[^/]+/){2})+)([^/]+)/?$ $1?$2                 [QSA,L]
RewriteRule ^([^/]+/.*/)([^/]+)/([^/]+)/?$ $1?$2=$3               [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?varfixa1=$1&varfixa2=$2 [QSA,L]
RewriteRule ^([^./]+)/*$ index.php?varfixa=$1                     [QSA,L]

You can test here:
link

    
20.10.2016 / 10:32