bklups
bklups

Reputation: 117

Two RewriteRules interfering, only one works at a time

On htaccess file with two RewriteRules, each works alone, but not both together

RewriteRule  ^([1-9]+)/.*/(.*) /sortir/index.php?com=page1&t=$1&l=$2 [QSA]

RewriteRule  ^([1-9]+)/([1-9]+)/.*  /sortir/index.php?com=page2&t=$1&v=$2 [QSA]

If I delete first, second works.
If I delete second, first works.

The link called for the first is like :

http://example.com/33/xxxx/city so $1 is 33 and $2 is city

The link called for the second is like :

http://example.com/33/432/xxxx/city/yyyyy so $1 is 33 and $2 is 432

Upvotes: 0

Views: 116

Answers (2)

David Ravetti
David Ravetti

Reputation: 2040

Although, as anubhava notes, more details would be helpful, there are at least a few issues I can point out with your current rules.

First, reverse the order of the rules. The second rule is less general since it starts with two sections of numbers and additional sections of text. Match that first, then match the more general rule.

Second, end each rule with the L flag, otherwise processing will continue to the second rule after the first is finished.

Third, update your matches so that they don't match a slash. This forces the pattern to match the exact directory structure you're looking for rather than matching any arbitrary number of directory levels.

With those things in mind, here are some updated rules to play with:

First rule matches http://example.com/33/432/xxxx/city/yyyy

Second rule matches http://example.com/33/xxxx/city

RewriteRule  ^([1-9]+)/([1-9]+)/[^/]+/[^/]+/.*  /sortir/index.php?com=page2&t=$1&v=$2 [QSA, L]
RewriteRule  ^([1-9]+)/[^/]*/([^/]*)$ /sortir/index.php?com=page1&t=$1&l=$2 [QSA, L]

If this is not the exact rule set you need, it should at least get you closer.

Upvotes: 1

anubhava
anubhava

Reputation: 785186

Both conditions are overlapping since this regex:

^([1-9]+)/.*/(.*)

will also match

^([1-9]+)/.*/(.*)

hence only one will fire. Why don't you explain your requirements clearly then we can help you write RewriteRule in unambiguous manner.

Upvotes: 1

Related Questions