0

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

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

14 Comments

You should also add the L flag. Is %{DOCUMENT_ROOT}/index2.php the correct path?
Commonly with those rules you usually want to add RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME} !-f ( for both )
You can use the RewriteRule flag S|skip to tie multiples RewriteRules to a single RewriteCond. Otherwise you need to repeat the same RewriteConds for both RewriteRules.
I'll do it tomorrow morning, I'm on mobile right now; keep in mind this: stackoverflow.com/questions/25520448/…
What you mean "for the root"? Is there an index page?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.