Daan
Daan

Reputation: 13

Check if other file then request exists in .htaccess

I'm trying to accomplish the following in a .htaccess file, but can't seem to get it to work...

The first step is where it goes wrong, I can't seem to figure out a way to check whether or not a file exists, that is not currently being requested. I've been looking at a lot of different things, also RewriteMaps, but can't seem to get to redirect from rewritemaps, or that's just not clear to me. For instance if I write as simple perl script that checks if a file exists, how do I get the result from that to whether or not to redirect?

Thanks for any help/tips!

Upvotes: 1

Views: 1424

Answers (2)

clmarquart
clmarquart

Reputation: 4721

I'm not sure what you have already, but this should work. You were probably getting the 500 error when using the -f because of a internal redirect loop.

RewriteEngine on

RewriteCond %{REQUEST_URI} !/maintenance.htm
RewriteCond %{DOCUMENT_ROOT}/maintenance.htm -f
RewriteRule (.*) /maintenance.htm [L,R]

RewriteCond %{REQUEST_URI} /maintenance.htm
RewriteCond %{DOCUMENT_ROOT}/maintenance.htm !-f
RewriteRule (.*) /index.php [L,R=301]

Hope this helps.

Upvotes: 3

Tobias
Tobias

Reputation: 7380

Maybe RewriteCond %{REQUEST_FILENAME} !-f will help to check for file existance

'-f' (is regular file) Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.

See: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Upvotes: 0

Related Questions