Check if a file exists in a directory or request another
Is there any .htaccess rule that checks if requested file does not exist in "mydir" and get index.php?get=mydir
I tried this but not working:
RewriteCond mydir/%{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ /index.ph开发者_如何学Cp?get=$1 [L]
If you're using Apache, I would suggest using this:
http://httpd.apache.org/docs/2.0/mod/mod_actions.html
This may not be the best approach but it works for me.
Basically you will setup a script that Apache will send all requests through.
# Files of a particular file extension
AddHandler handler .html
Action handler /handler.php
Each time a html file is requested, Apache will run handler.php.
Inside handler.php
, you can use $_SERVER
to determine the requested file, check if it exists and include it if it does exits. If it doesn't exist, do whatever you need to do.
To serve a page if it exists else serve other one, add the following to your .htaccess file,
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule .+ - [L]
# serve file if exists
RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f
RewriteRule ^(.*)$ /cache/index.html [L,QSA]
# else serve other file
RewriteCond %{DOCUMENT_ROOT}/cache/index.html !-f
RewriteRule ^(.*)$ /index.html [L]
Do not forget that for this to work you should have AllowOverride All in your vhosts file.
精彩评论