My regex code works well with second variable, the ([^&]+)(&.*) which is after myvar= , but i can’t get the 3rd, which is after foo= (same as 2nd).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?myvar=([^&]+)(&.*)?&foo=([^&]+)(&.*) [NC]
RewriteRule ^(index\.php)?$ /site/myvar/%2/%3/? [R=301,L]
That outputs the url as follows:
http://localhost/site/myvar/15//
So how to output the 3rd variable between the last 2 backslashes?
What you want is the fourth capture, not the third. The third one is
(&.*)even if that is empty. To determine the number, you simply count opening parentheses from left to right.