Checking a File Exists in a virtual directory

Using mod_rewrite to handle various content issues

Checking a File Exists in a virtual directory

Postby baynezy » Fri Oct 10, 2008 3:38 am

OK,
I have a 3 web servers behind a load balancer and I am working on a solution to serve HTML versions of dynamic pages if the files exist. Because of the clustered environment I have created these on the SAN and referenced them as virtual directories.

This is where the problem begins, I don't want to hard code the path as I want this code to work on my dev, staging and UAT boxes as well as in production and it appears that to use RewriteCond with the -f flag it requires the full qualified path and not the URL. There is obviously the %{DOCUMENT_ROOT} variable that can dynamically find the path to the web root, but is there a way to get the path to a virtual directory?

The other option I investigated was setting the server path as an environment variable in httpd.conf so it is server wide. I used SetEnv for this but it does not seem to be accessible from the .htaccess file which is where my rewrites are. I am really stumped and would appreciate some help.

Regards,

Simon
baynezy
 
Posts: 7
Joined: Tue Sep 11, 2007 1:46 am

Postby richardk » Fri Oct 10, 2008 12:24 pm

What do you mean by Virtual Directories? You will have to look in the environmental variables (probably with the server-side language you are using, like the phpinfo() command).

The other option I investigated was setting the server path as an environment variable in httpd.conf so it is server wide. I used SetEnv for this but it does not seem to be accessible from the .htaccess file which is where my rewrites are.

Try %{ENV:variable_name}.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby baynezy » Mon Oct 13, 2008 6:01 am

I mean that yo have the web root, but then you can create an alias ofr a directory that is somewhere else. In this instance a directory on my SAN (Storage Area Network). So for example http://www.mydomain.com/ goes to the web root, but after creating an alias called 'example' pointing at a folder in my SAN then http://www.mydomain.com/example/ will load content from there.

I am using environment variables to make it work, but I would rather use something more elegant and maintainable.

blocks of code like this are really not manageable!

RewriteCond %{HTTP_HOST} ^dev\.mydomain\.com [NC]
RewriteRule .* - [NC,E=CACHEPATH:\\\\212.125.224.220\\d$]
RewriteCond %{HTTP_HOST} ^(staging|uat1)\.mydomain\.com [NC]
RewriteRule .* - [NC,E=CACHEPATH:\\\\192.168.20.220\\d$]
RewriteCond %{HTTP_HOST} ^(testing|uat2)\.mydomain\.com [NC]
RewriteRule .* - [NC,E=CACHEPATH:\\\\192.168.11.166\\media]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com [NC]
RewriteRule .* - [NC,E=CACHEPATH:\\\\192.168.11.166\\media]
baynezy
 
Posts: 7
Joined: Tue Sep 11, 2007 1:46 am

Postby richardk » Mon Oct 13, 2008 9:40 am

An Apache Alias? there are no variables specifically set. You may be able to use the REQUEST_URI variable to find out the first directory. Is there a variable with the IP address in it (possible SERVER_ADDR)?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby baynezy » Mon Oct 13, 2008 11:18 am

Yes it is exactly that an Apache Alias.

The problem is that the SAN is a completely different machine so picking up variables from the machine Apache is installed on is not going to help.

I think I need to give up on making this work using a dynamic variable. It seems that setting a server level variable is the way to go but as I mentioned before I have tried setting an environment variable in the httpd.conf but it does not seem to be visible to the .htaccess. Is there a way to create a server wide variable that I can reference in my .htaccess file?

Thanks.
baynezy
 
Posts: 7
Joined: Tue Sep 11, 2007 1:46 am

Postby richardk » Tue Oct 14, 2008 9:13 am

Did you try %{ENV:variable_name}? Also try adding REDIRECT_ as a prefix to your variable name.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Content

Who is online

Users browsing this forum: No registered users and 13 guests

cron