Problems with regular expressions (friendly url)

7

I'm having trouble reading a product's code from a friendly URL.

With the regular expression that I put, it is accepting all the characters that are in front of the product code, that is, typing produto/9789/quantidade/5 it understands the product code as 9789/quantidade/5 , not just 9789 .

Could anyone help me put this regular expression correctly?

Here's the one I used:

RewriteRule ^carrinho\/produto\/(.+)?$ carrinho.php?produto=$1&quantidade=1 [NC,L]
RewriteRule ^carrinho\/produto\/(.+)\/quantidade\/([0-9]+)\/?$ carrinho.php?produto=$1&quantidade=$2 [NC,L]

Another problem is that when the user places a / at the end of the product code on the first URL it goes along to the product code.

    
asked by anonymous 23.12.2014 / 13:46

3 answers

2
^carrinho\/produto\/(.+)?$

Means capturing carrinho/produto/ optionally followed by anything until the end of the line. The problem is this anything . By default, it can be 9789/quantidade/5 .

Rewrite limiting to being just numbers. So:

^carrinho\/produto\/(\d+)?$

Or if you prefer:

^carrinho\/produto\/([0-9]+)?$
    
23.12.2014 / 15:01
2

The first expression can be set to not capture / in the product:

^carrinho\/produto\/([^\/]+)?$

So it will not hit the long URLs that have quantity, leaving them for the second expression, which seems to be working fine.

    
23.12.2014 / 21:14
1

I do not know if it is useful to you, but here is an expression to search only the past parameters of the url, I did not test with several urls just what you went through.

Regex

(\w+=([^&]*))

Explanation

\w+= = alnum - letras a números - 1 ou mais - seguido de =
[^&]* = tudo exito & - se quiser limitar para 'alnum' substiruir por [\w]*, - 0 ou mais

Changes

This Regex will return you an array as you can see here .

Caso queira um array simples de 'tipo=dado' elimine o parenteses interno (\w+=[^&]*)
Caso queira um array de 'dados' elimine o parenteses externo \w+=([^&]*)
    
26.12.2014 / 12:54