9903

RewriteRule question

Question:

RewriteRule oferte-cel-mai-bun-pret(-(apartamente|garsoniere|case|vile|birouri|spatii-comerciale|spatii-industriale|mini-hoteluri-pensiuni|ferme|proprietati-speciale|terenuri-intravilane|terenuri-extravilane))?(-(([0-9+])-([a-z-+]+)+))?-(vanzare|inchiriere)/([a-z-+]+)(/pagina-(([0-9]+)+))?(/)?$ index.php?seo[]=anunturi&tip_categorie=$2&nr=$5&tip_numar=$6&cmbp_$7=1&tip_cautare=$7&judet=$8&pagina=$9 [L,QSA]

I have this RewriteRule, and this url: <a href="http://site/oferte-cel-mai-bun-pret-vile-1-etaj-vanzare/bucuresti/pagina-2/" rel="nofollow">http://site/oferte-cel-mai-bun-pret-vile-1-etaj-vanzare/bucuresti/pagina-2/</a>

All parameters are ok untill last one about page, i receive:

Array ( [seo] => Array ( [0] => anunturi ) [tip_categorie] => vile [nr] => 1 [tip_numar] => etaj [cmbp_vanzare] => 1 [tip_cautare] => vanzare [judet] => bucuresti [pagina] => /pagina-2 )

[pagina] => /pagina-2 - i want here to return only page number if isset in url.. if not to return it blank..

Problem is here .. &pagina=$9 :| I can`t put $10 as a parameter? more than 9... ?

Answer1:

You don’t need to group every single atom in your pattern. For example, (-(([0-9+])-([a-z-+]+)+))? can probably be reduced to (-([0-9+])-([a-z-+]+))?:

RewriteRule\ ^oferte-cel-mai-bun-pret(-(apartamente|garsoniere|case|vile|birouri|spatii-comerciale|spatii-industriale|mini-hoteluri-pensiuni|ferme|proprietati-speciale|terenuri-intravilane|terenuri-extravilane))?(-([0-9+])-([a-z-+]+))?-(vanzare|inchiriere)/([a-z-+]+)(/pagina-([0-9]+))?/?$\ index.php?seo[]=anunturi&tip_categorie=$2&nr=$4&tip_numar=$5&cmbp_$6=1&tip_cautare=$6&judet=$7&pagina=$9 [L,QSA]

With this you can reduce 12 groups to just 9.

And if you are using Apache 2, you can use non-capturing groups (?:…) instead of normal groups (…). The matches of non-caputring groups cannot be referenced. So use normal groups for everything that you want to be able reference and use non-capturing groups for everything else:

RewriteRule\ ^oferte-cel-mai-bun-pret(?:-(apartamente|garsoniere|case|vile|birouri|spatii-comerciale|spatii-industriale|mini-hoteluri-pensiuni|ferme|proprietati-speciale|terenuri-intravilane|terenuri-extravilane))?(?:-([0-9+])-([a-z-+]+))?-(vanzare|inchiriere)/([a-z-+]+)(?:/pagina-([0-9]+))?/?$\ index.php?seo[]=anunturi&tip_categorie=$1&nr=$2&tip_numar=$3&cmbp_$4=1&tip_cautare=$4&judet=$5&pagina=$6 [L,QSA]

Answer2:

pagina=$10 instead of pagina=$9 ?

Recommend