The solution below worked for me.
RewriteEngine on RewriteBase / #rule1 #Guard condition: only if the original client request was for index.php RewriteCond %{THE_REQUEST} ^[AZ]{3,9}\ /index\.php [NC] RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$ [NC] RewriteRule . /index/%1/%2/? [L,R] #rule 2 RewriteRule ^index/(\d+)/(\d+)/$ /index.php?id=$1&cat=$2 [L,NC]
Thats what i think is going on
From the above steps
- Go to index.php? id = 3 & cat = 5
- See location line read index / 3/5 /
- Content contained in index.php? id = 3 & cat = 5
In step 1, Rule 1 matches and redirects to the location bar and performs step 2.
In step 3, rule 2 now matches and is rewritten in index.php.
The rules are repeated for Davidโs reasons, but since THE_REQUEST unchanged after setting the original request, it still contains /index/3/5 THE_REQUEST , so Rule 1 does not match.
Rule 2 does not match any, and the result of index.php is served.
Most other variables change, for example. REQUEST_URI Their modification during the processing of rules and the incorrect expectation of a pattern matching against the original request are a common cause of infinite loops.
She seems quite esoteric sometimes, but I'm sure there is a logical reason for its complexity :-)
EDIT
Of course there are two different requests
There are 2 client requests, the source from Step1 and one of the external redirects in step 2.
What I overshadowed above is that when Rule 2 matches the second request, it is rewritten in /index.php and causes an internal redirection. This forces you to reload the .htaccess file for the / directory again (it could easily be another different directory with different .htaccess rules) and run all the rules again.
So ... why does this lead to a request loop when I select the first rule?
When rules re-run, the first rule now unexpectedly matches the result of rewriting Rule2 and redirects, causing an infinite loop.
Davidโs response contains most of this information, and I mean โfor the reasons David gave.โ
However, the main thing is that you need an additional condition, either your condition, which stops the further processing of the rules in internal redirects, or mine, which prevents the coordination of rule 1, is necessary to prevent an infinite loop.