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

John Smith
  • 6,129
  • 12
  • 68
  • 123

1 Answers1

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]
Federkun
  • 36,084
  • 8
  • 78
  • 90
  • Forbidden: You don't have permission to access / on this server. – John Smith Dec 12 '16 at 11:14
  • 1
    You should also add the `L` flag. Is `%{DOCUMENT_ROOT}/index2.php` the correct path? – Federkun Dec 12 '16 at 11:15
  • breaking: this applies to images, so the design is broken. Is there any way to define exceptions by file extension? – John Smith Dec 13 '16 at 22:58
  • 1
    Commonly with those rules you usually want to add `RewriteCond %{REQUEST_FILENAME} !-d` and `RewriteCond %{REQUEST_FILENAME} !-f` ( for both ) – Federkun Dec 13 '16 at 23:05
  • 1
    You can use the RewriteRule flag S|skip to tie multiples RewriteRules to a single RewriteCond. Otherwise you need to repeat the same `RewriteCond`s for both `RewriteRule`s. – Federkun Dec 13 '16 at 23:19
  • sorry I cant make it work :S could you make a pastebin example for me? – John Smith Dec 13 '16 at 23:24
  • 1
    I'll do it tomorrow morning, I'm on mobile right now; keep in mind this: http://stackoverflow.com/questions/25520448/multiple-rewriterule-to-one-rewritecond – Federkun Dec 13 '16 at 23:25
  • now the images works but for the root, I get "Forbidden You don't have permission to access / on this server." – John Smith Dec 14 '16 at 10:20
  • 1
    What you mean "for the root"? Is there an index page? – Federkun Dec 14 '16 at 10:35
  • if I just visit the on localhost, without any URL parameters. The entryPont.php is available – John Smith Dec 14 '16 at 10:36
  • 1
    Is there an index.php, index.html, ..., that the webserver doesn't have the permission to read? – Federkun Dec 14 '16 at 10:40
  • no, there is no index.php, index.phtml and its Windows. You also used "entryPoint*.php" files – John Smith Dec 14 '16 at 10:41
  • to summary up, the rules again. I want to have direct access to .png, .jpg, .gif, .jpeg, .bmp, .ico, .flv, .mpeg, .mp4, .mp3, .swf files. Any other case, site should look for entryPoint2.php and execute if exists. If not, execut entryPoint.php – John Smith Dec 14 '16 at 10:44
  • 1
    Usually is more easy to say that with "RewriteCond %{REQUEST_FILENAME} !-f", that's mean "only if the file requested doesn't exists". You can add another condition and exclude index.php – Federkun Dec 14 '16 at 10:52