Configure N levels in htaccess [duplicate]

3

On the site we have the product area where there may be N levels and sub-levels of categories. Currently htaccess is configured to accept 2 levels:

RewriteRule ^([a-zA-Z_-]+)/produtos/([^/]*)/([^/]*)/([^/]*) index.php?area=produtos&lang=$1&n1=$2&n2=$3&n3=$4 [NC,QSA,L]
RewriteRule ^([a-zA-Z_-]+)/produtos/([^/]*)/([^/]*) index.php?area=produtos&lang=$1&n1=$2&n2=$3 [NC,QSA,L]
RewriteRule ^([a-zA-Z_-]+)/produtos/([^/]*) index.php?area=produtos&lang=$1&n1=$2 [NC,QSA,L]

The URL looks like this:

  

localhost / en / products / 1-shoes / 5-leather / 15-shoeXPTO
  localhost / en / products / 1-shoes / 5-leather
  localhost / en / products / 1-shoes

It works perfectly for 2 levels of categories, however I want to eliminate this limitation and allow N category levels.

How do I pass to index.php what's ahead of "products /" regardless of how many levels?

    
asked by anonymous 04.11.2014 / 12:51

1 answer

0

In fact you will need only one rule in your HTACCESS file and the rest you will do in your PHP script, like this:

In your .htaccess

RewriteRule ^produtos/(.*)\.html?$ index.php?area=produtos&vars=$1 [NC,L]

In your php script:

/**
 * Verifica se a variável vars existe
 * se ela existir explode gerando um array
 * caso contrário retorna vazio
 **/
$vars = isset($_REQUEST['vars']) ? explode('/',$_REQUEST['vars']) : '';

// Separando as variáveis
$n0 = isset($vars[0]) ? $vars[0] : '';
$n1 = isset($vars[1]) ? $vars[1] : '';
$n2 = isset($vars[2]) ? $vars[2] : '';
...
$nx = isset($vars[x]) ? $vars[x] : '';

Or to capture the variables more dynamically you can do:

foreach ($vars as $k=>$var){
    $n = 'n' . $k;
    $$n = $var;// cria a variável na execução $n0, $n1, $n2, $n3 ... $nx
}

Good luck!

    
30.11.2014 / 01:40