Htaccess does not correctly validate the redirect rule

Asked

Viewed 50 times

0

Has in htaccess in which the regex works perfectly without numbers in the rule, but with numbers does not work properly.

RewriteRule ^([a-zA-Z]{1,20})-to-([a-zA-Z]{1,20})-([a-zA-Z0-9-]{1,30})\/?$ page.php?ti=$1&at=$2&pl=$3 [NC,L]//linha anterior
RewriteRule ^([a-zA-Z]{1,20})-to-([a-zA-Z]{1,20})-([a-zA-Z0-9-]{1,30})-([0-9]{1,3})\/?$ page.php?ti=$1&at=$2&pl=$3&pagination=$4 [NC,L]

The previous line is the page and the second line is the page with pagination. if the third rule, both in the first and second line, is a-zA-Z- (with the stroke at the very end) everything works beauty, but if it is a-zA-Z0-9- does not work as it should.

the page URL is something like this:

www.exemplo.com/bla-to-ble-nome-com-numero-2

page url with paging

www.exemplo.com/bla-to-ble-nome-com-numero-2-3

nome-com-numero-2 refers to Part 3 of the regex

1 answer

1

The reason is that he is not falling into rule 2 but into rule 1.

Look at ([a-zA-Z0-9-]{1,30}) this passage contemplates exactly nome-com-numero-2-3

To solve this problem you must change the order of the rule, so you start from the most specific, to the least specific.

RewriteRule ^([a-zA-Z]{1,20})-to-([a-zA-Z]{1,20})-([a-zA-Z0-9-]{1,30})-([0-9]{1,3})\/?$ page.php?ti=$1&at=$2&pl=$3&pagination=$4 [NC,L] // regra mais especifica
RewriteRule ^([a-zA-Z]{1,20})-to-([a-zA-Z]{1,20})-([a-zA-Z0-9-]{1,30})\/?$ page.php?ti=$1&at=$2&pl=$3 [NC,L]// regra menos especifica
  • Thanks @Guilhermelautert, this way the most specific is working, already the least specific (without paging) no error appears, but the fields are empty (gets that search the values in the url)

  • @Gisele now have no time to develop the solution, but the reason is that even changing the more specific order also contemplates the string minor, would have to refactor the REGEX.

Browser other questions tagged

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