A short article how to take the pain from your Apache by extracting a generic Rewrite Ruleset from a ‘large’ .htaccess and put it to the server configuration, but keeping it ‘dynamic’.

As I promised, I am posting a solution to my .htaccess problem from Part 1.
It’s just a simplified example for you guys to get an idea of this simple, but mighty solution.
Note: If you considering to put this in a .htaccess file, then I will send my friend Achmed after you. Who will come and keel you.
First we need to put a Ruleset to our Apache configuration, global or VirtualHost container, depends on what you need. ;-)
I am currently still building a dedicated solution with RewriteCond matching, which will follow asap. ;-)
#httpd.conf or example_com.conf
:1 RewriteEngine On
:2
:3 RewriteMap action txt:/var/www/example.com/maps/action.txt
:4 RewriteMap content txt:/var/www/example.com/maps/content.txt
:5
:6 RewriteRule ^/([a-z0-9_]+)/([a-z0-9_]+)\.html$ http://%{HTTP_HOST}/index.php?action=${action:$1}&cid=${content:$2} [P]
#action.txt
:1 edit 1
:2 view 2
:3 show 3
:4 filter 4
:5
#content.txt
:1 rewrite_mapping_for_absolute_dummies 55511
:2 my_super_duper_dummy_content 55522
# sample index.php to check values
:1 <pre>
:2 <?php
:3 var_dump($_GET);
:4 ?>
:5 </pre>
After you created the files you can access
'http://blog.example.com/view/rewrite_mapping_for_absolute_dummies.html'
which will be rewritten to
'http://blog.example.con/index.php?action=view&cid=55511'
The PHP script will output:
array(2) {
["action"]=>
string(1) "2"
["cid"]=>
string(5) "55511"
In the example I ‘defined’ a required sample structure, which is basically:
http://hostname/action/content_seo.html
The Ruleset is taking the ‘action’ and matching the ‘key’ with the ‘value’ inside the defined RewriteMap. If it’s matching the value will taken from the Map and put into the Rule. The same thing goes for the ‘content’ map, where a string is matched and a ‘id’ is returned.
The effect is, that we have dynamic Rulesets, which we can modify when ever we want and the Rulesets are re-cached everytime a mapfile had changed.
Therefore we profit from the voodoo magic of mod_rewrite without having the performance impact as we have when we are using large .htaccess files.
Part 3 is on it’s way and will cover a more complex example with a ‘special feature’. Well basically it’s just an improvement to this example. ;-)