I’ve been using XAMPP as the web server for my iPhone web application. XAMPP is great because ot runs on my Windows machine, is easy to control right there and then (no switching machines etc), has a nice simple directory structure and most importantly, is pretty much unlocked out of the box, so you can do all sorts of stuff like running cgi scripts in your main application directory instead of having to put them in cgi-bin.
Of course the real world is not so friendly so it’s time to look at making my web application more ‘real world’ ready and that means moving the scripts off to cgi-bin.
One issue I had is that my Perl scripts need access to various configuration files and those files are off elsewhere. When I was running everything from one directory things were easy as the files were in the same directory as the scripts. Now I want to make it ‘look’ like it’s all still running from the applications directory (under document root) but send the script requests off to cgi-bin.
So the first step is to redirect the script requests to my applications directory to the cgi-bin directory. To do that I simply added a RewriteRule to my .htaccess file as follows:
RewriteRule ^(.*)\.pl(.*) /cgi-bin/$1.pl$2 [NC,L]
That works fine but I also have an environment variable I set in .htaccess that tells the scripts where to find the configuration files they need to read. The problem is that when the redirect is done the environment variable is no longer passed, or so I thought.
Some experimentation showed that the variables are actually passed but the name is prefixed with REDIRECT_. So if my variable in .htaccess is called for example MYAPPVAR, when the script in cgi-bin runs, it can access the variable as REDIRECT_MYAPPVAR.
This means I can now create two .htaccess files that the user can choose from. One to let the scripts run locally as I do for testing using XAMPP, and one for use when the scripts are moved to cgi-bin.
All I need to do is to modify my scripts to try to obtain the variable using the regular name first (MYAPPVAR) and if that’s not found, try again using REDIRECT_MYAPPVAR.
The result of all this jiggery pockery is that a request for say:
Gets redirected to:
While the MYAPPVAR environment variable from the .htaccess file in the ‘myapp’ directory is available to the script as REDIRECT_MYAPPVAR.