Apache mod_rewrite

Asked

Viewed 107 times

2

I have a rewrite rule I found in a very old question of the US:

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

This rule allows 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&...

Note that the first parameter passed becomes the name of the php file, the others go from two to two and become 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 I could even do that if it were only regex, but the .htacces has some different things.

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

/test/foo/bar/baz/u/e/...     --->
/index.php?varfixa1=test&varfixa2=foo&bar=baz&u=e&...
  • What do you expect when there are an odd number of parameters?

  • 1

    Probably the worst that can happen in this case is the last variable to be empty, so I simply ignore it, or if it is necessary I display an error on the screen. Unless that odd number is 1, then I expect you to go to the page with no GET parameter

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


A way that can help me too would always be to send to a 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:
http://mariano.freevar.com/htaccess/test/foo/bar/baz/u/e/v/f

  • Perfect! That’s exactly it! Know if you have any advantage/disadvantage in using index.php to redirect to other pages or include them or if it’s just a matter of taste?

  • I just noticed I can’t send / to a string. When I try to use %2F makes a mistake.

  • @Guilherme I can’t seem to allow them. Not even with flag B.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.