如果install.php存在,则访问install.php,否则访问index.php

Before accessing my php script, user needs to access install.php and configure the script. The install.php will delete itself after completion. After install.php is deleted, user must be able to access index.php

What I have tried:

RewriteEngine On
RewriteBase /myscript/
RewriteRule ^ install.php [E]

I am able to redirect the user to install.php but not to index.php if install.php is not present.

I dont know the RewriteCond to check if install.php is present:

RewriteEngine On
RewriteBase /myscript/
RewriteCond ?? ??
RewriteRule ^ install.php [E]

Any help is appreciated. BTW I am not at all good with .htaccess configurations.

UPDATE :

Thank you for comments and replies. Yes I know that I can redirect through my php script like:

if(file_exists('install.php')){
     header('Location: http://'.$domain.'/install.php');
}

But can we do the same with .htaccess ?

Try adding this to an .htaccess file in the myscript folder:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/myscript/install.php -f
RewriteRule ^ install.php [L]

Explanation

  • The condition checks if the file install.php exists in the myscript folder
  • If the condition is met, the rule will rewrite any request (since the beginning of string anchor ^ will match any file) to install.php

I would try to check with that method if the file exists

bool file_exists ( string $filename )

For a pure .htaccess solution:

RewriteCond %{DOCUMENT_ROOT}/myscript/install.php -f
RewriteRule ^ /myscript/install.php [L]

You can use DirectoryIndex to achieve what you want (docs). Have this in the .htaccess in the root directory of the application.

DirectoryIndex install.php index.php

RewriteEngine on
RewriteBase /myscript/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewrietRule . index.php [E]

If the user goes to the directory the application is installed in, e.g. http://example.com/myscript/, it will first try to load install.php, then if that does not exist, it will try to load index.php. If the user goes to http://example.com/myscript/index.php, you are out of luck, but it does not make sense to go to that url! If it is a concern to you, you are better of using romaneso's answer, loading index.php all the time and loading install.php from there if required.