Apache,.htaccess,多索引页面

I want to a file which executed at EVERY request, lets say index.php

RewriteRule ^(.*)$ index.php [QSA]

however, if it happens to have index2.php, lets rather execute that file. In other words, if index2.php exists, it overrides the default behaviour. So I would write this:

RewriteRule ^(.*)$ index2.php [QSA]
RewriteRule ^(.*)$ index.php [QSA]

but if index2.php doesnt exists, it fails

RewriteRule ^(.*)$ index2.php [L]
RewriteRule ^(.*)$ index.php [L]

this way too. Index2.php doesnt need to be exists, but index.php does and will always be

You can add a condition, and apply the RewriteRule only when index2.php exists.

RewriteCond %{DOCUMENT_ROOT}/index2.php -f
RewriteRule ^(.*)$ index2.php [QSA,L]