Home > General Stuff > More RewriteRule fun…

More RewriteRule fun…

For development under XAMPP it’s been convenient to keep all my perl scripts in the same directory as the rest of the applications files such as jpegs etc. However in the real world cgi scripts typically live elsewhere on the web server so I wanted to come up with some simple mechanism that I could use to redirect the script requests to the servers cgi-bin directory when the application was installed on a ‘real’ server, but still be able to leave the scripts in the applications directory (or a sub directory) for development since it makes it easy to simply copy everything between machines as I move stuff back and forth between my work machie and my PC at home. The whole point of this is to make it easier for someone to install and configure the application on a real web server at some point since there’s nothing more frustrating than having software fail to work because you’ve made it too hard for the user to configure the dang thing.

After a ridiculous amount of experimenting (this stuff is harder than it looks) I came up with the following rewrite rules for my .htaccess file that would, with a little teaking, give me a sort of dual mode capability. Basically, if the script files are in my applications cgi-bin sub directory than it executes them from there which is great for development and testing, especially under XAMPP. But, if the file is not in the local cgi-bin directory, or the directory is simply not there, then it tries to run the script from the server’s standard cgi-bin directory.

Thus by copying the script files to the server’s cgi-bin directory and simply renaming my local cgi-bin directory so it’s not found, the server switches from running the local copies of the scripts in the application’s cgi-bin directory, to the ones in its cgi-bin directory.

My earlier work with the environment variables for the path to the applications configuration files means that regardless of where the scripts run they can still find the configuration files, whilst to the user, it looks like everything runs from the applications path under the server’s document root.

Here’s my Rewrite rules from my .htaccess file:

# Rule 1. If the requested file exists in the specified path, just go to it.
# Rule 2. If the specifed script exists in the applications local cgi-bin dir, redirect to that.
# Rule 3. Default rule. Redirect to script in standard cgi-bin dir (does not test if it exists or not though)

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.pl(.*) $1.pl$2 [NC,L]

RewriteCond /xampp/htdocs/myApp/cgi-bin/$1.pl -f
RewriteRule ^(.*)\.pl(.*) /myApp/cgi-bin/$1.pl$2 [NC,L]

RewriteRule ^(.*)\.pl(.*) /cgi-bin/$1.pl$2 [NC,L]

Unfortunately it looks like it’s not so easy to conditionally redirect the ErrorDocument and DirectoryIndex directives so you still need to manually edit the .htaccess file to specify the paths to those but this technique at least cuts down on the number of changes required.

OK, so it’s not perfect but it is a step in the right direction.

Categories: General Stuff
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment