As I built Tsugi, I want to ship with a decent, working .htaccess
in folders that need it. My most typical use case is that I want to map all the URLs in a folder into a file like index.php
.
There are two good ways to do this. The old standby is a long set of mod_rewrite
rules. The new, much more elegant trick is to use FallbackResource
in mod_dir
in later versions of Apache 2.2.
The problem is that clever hosting providers upgrade to the new Apache and then figure they can remove mod_rewrite
so you know how to do it in either case but don’t have a good way to trigger when to use what approach.
This is my approach that I use in Tsugi when I want to map all URLs to one file:
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^ - [E=protossl] RewriteCond %{HTTPS} on RewriteRule ^ - [E=protossl:s] RewriteRule "(^|/)\." - [F] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^ index.php [L] </IfModule> <IfModule !mod_rewrite.c> FallbackResource index.php </IfModule>
It is not perfect but kind of deals with things as the move forward. If mod_rewrite
is there – use it – it works in later Apache versions as well but if mod_rewrite
is there, use it and if not, hope that FallbackResource
is there.
Now of course there are some Apache versions / setups where this fails – but on average, over time as Apache’s get upgraded, things get simpler and over time the mod_rewrite
code just will stop activating.
I also added this information to a Stack Overflow question.