I'm trying to accomplish the following in a .htaccess file, but can't seem to get it to work...
- if file ~/maintenance.htm exists, temporarily redirect to that file (R=302) and append the current query string (QSA), end (l)
- if maintenance.htm file is requested (refresh by user) and exists, show the file, query string and end (L, QSA)
- it maintenance.htm file is requested (refresh by user) and no longer exists, redirect to index.php (R=301) and append the query string (QSA), end (L)
The first step is where it goes wrong, I c开发者_StackOverflow社区an'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!
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.
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
精彩评论