问题
Im playing arround with cache. What I want is the following logic:
if ( file_exist(/cache/$request_uri.txt) ){
1. show that file
2. Stop all rewrite actions, but perform other actions (like charset, error_pages)
}
else{
do whatever you normally do
}
Small example. I have the following files
http://domain.com/example-123.htm (the requested page)
http://domain.com/cache/example-123.htm.txt (the cache-version of that page)
http://domain.com/someDir/example-456.htm (the requested page)
http://domain.com/cache/someDir/example-456.htm.txt (the cache-version of that page)
Instead of getting the index.php to parse the url and build the page, I want to show that file and stop.
I though this would do it, but it doesn't:
RewriteCond ^cache%{REQUEST_URI}\.txt -f # check if the file exists in cache dir
RewriteRule ^(.*) /cache/$1\.txt [L] # i so, rewrite rule to it and stop
The [L]
does, according to my cheatsheet, "Last - stop processing rules". If that only means the rewrite rules, than thats what I need.
I cant get it to work, I could use a push in the right direction :)
I've marked an answer as solution it, did exaclty what it should. The reason I want this in the .htaccess file is because this way my index.php doesn't get called, nor does the database. A very fast and light method.
However, this creates a new problem: Some items (like the menu) can change (often). That would mean I'd have to delete all cache files every change, which prohibits it from working efficient.
To tackle this problem, im going to see if I can use some clever .shtml files to fix that problem (might need to allow php to woth in shtml files).
I'll update this post as soon as I've got something nice working for those interested
回答1:
You can try this rule:
# check if the file exists in cache dir
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}cache/$1.txt -f
RewriteRule ^(.+?)/?$ /cache/$1.txt [L]
来源:https://stackoverflow.com/questions/19973482/htaccess-show-file-if-in-cache-directory-if-not-continue