1 (edited by slinger 2014-01-07 16:34:05)

Topic: [OK] Unable to check file existence using RewriteCond in Apache

Update: Problem (might be) solved! My complete .htaccess can be found in the last post. I am still not sure if the full web path might change in the future, but it works for the moment. The following is my old question:


Hello everybody!

First of all I want to thank everyone at TuxFamily for an amazing service! Apart from the usual project hosting (like git and download repository), the website features (like php and mysql) are simply unbelievable!

Now to the problem:

I decided to try to implement caching to the website for my project, in order to minimize database and php usage. While the website is lightweight and got few visitors I still want to do as much as I can to minimize any load. When the (php generated) pages are visited, static pages are created in a subdirectory called "cache" and the plan is to check for existence of these static files using RewriteCond in ".htaccess" and redirect to them using RewriteCond.

In short: I have been able to successfully check for file existence using:

RewriteCond ${REQUEST_FILENAME} -f

But since I need to check for files in a modified path (the static files are stored in a subdirectory), I need to use the %{DOCUMENT_ROOT} and ${REQUEST_URI} variables (to add "/cache/" between).

The problem is: when trying to use "RewriteCond" without ${REQUEST_FILENAME} it always seems like the file is missing. For example, replacing the above with:

RewriteCond %{DOCUMENT_ROOT}${REQUEST_URI} -f

Should (in most cases) produce the same behaviour, but instead fails for me.

I'm suspecting this might be an intentional security feature, but I thought I would ask and see if I were doing something wrong?

Thanks Again!

Re: [OK] Unable to check file existence using RewriteCond in Apache

First, did you forget to add "RewriteBase /" ?

%{DOCUMENT_ROOT} is set to "/data/web" which is pretty useless for you, you can however try to use %{DOCUMENT_ROOT_HASH}

Sylvain

3 (edited by slinger 2013-08-12 14:03:44)

Re: [OK] Unable to check file existence using RewriteCond in Apache

When I edited my last post it got a bit unorganized, so here's a shorter description of my current problem:

DOCUMENT_ROOT_HASH seems to be exactly what I was looking for, and I can obtain it through phpinfo(). But I seem unable to use it in ".htaccess".

A rule that matched positively in the htaccess was: "RewriteCond %{DOCUMENT_ROOT_HASH} ^$", which should mean it's not set/available?

I guess it's possible that htaccess only got a subset of all variables. Would it be okay to copy the string from %{DOCUMENT_ROOT_HASH} directly into the htaccess or is there a chance it might change? I know it's probably not a good idea, but I felt the need to ask.

Thanks again for such a wonderful service!


update: okay, looking at this thread on the forum, it seems like the path to the htdocs does not change and the contents of DOCUMENT_ROOT_HASH can be copied explicitly to the htaccess file. Is this correct?

Re: [OK] Unable to check file existence using RewriteCond in Apache

I decided to copy&paste the current value of %{DOCUMENT_ROOT_HASH}, since it unfortunately can not be used directly from .htaccess. But the path seems to remain unchanged so hopefully this "hard coded" solution will keep working.

Worst case scenario is that the path changes without me noticing, resulting in the cache checking failing. In that case every page and cache will be generated from php at every visit, resulting in a bit more load. But I will try to check the cache status from time to time, in case anything happens.

My full .htaccess file:

AddDefaultCharset UTF-8
Options -Indexes

RewriteEngine On
RewriteBase /


#funky cache:
# Check for cached index page from static cache folder.
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond /data/web/d3/fa/2d/recaged.net/htdocs/cache/index.html -s
RewriteRule ^$ cache/index.html [L]

# Check for other cached pages from static cache folder.
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond /data/web/d3/fa/2d/recaged.net/htdocs/cache%{REQUEST_URI} -s
RewriteRule (.*) cache%{REQUEST_URI} [L]
#


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Main URL rewriting.
RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]

5 (edited by xavier 2014-01-03 12:44:14)

Re: [OK] Unable to check file existence using RewriteCond in Apache

Hello,

Indeed, there are currently no plan to change the path you used -- it's computed, you know: the digits are taken from the MD5 hash of your webarea ( try echo -n recaged.net | md5sum in your shell)
Regarding your .htaccess, have you tried with %{ENV:DOCUMENT_ROOT_HASH} instead of %{DOCUMENT_ROOT_HASH}?

6 (edited by slinger 2014-01-07 16:43:26)

Re: [OK] Unable to check file existence using RewriteCond in Apache

Thanks for your reply!

First of all: Very interesting to hear about the path (automatic and based on hashing). I did not know that. I just assumed it was meant to provide certain ad-hock possibilities (migration/backup/raid).

I also did not know about that possibility of accessing environment variables through .htaccess. I've changed my rules (just in case the path ever does change) and it seems to work perfect! I hope it doesn't cause any big performance impact on apache (checking a global variable compared to a built-in)?


With the tweak applied, the following is now my current .htaccess:

AddDefaultCharset UTF-8
Options -Indexes

RewriteEngine On
RewriteBase /


#funky cache:
# Check for cached index page from static cache folder.
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{ENV:DOCUMENT_ROOT_HASH}/cache/index.html -s
RewriteRule ^$ cache/index.html [L]

# Check for other cached pages from static cache folder.
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{ENV:DOCUMENT_ROOT_HASH}/cache%{REQUEST_URI} -s
RewriteRule (.*) cache%{REQUEST_URI} [L]
#


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# Main URL rewriting.
RewriteRule ^(.*)$ index.php?WOLFPAGE=$1 [L,QSA]

Thanks again for all your help!