I’m trying to rewrite all request paths to my initiation file. Like so:
RewriteRule .* framework/init.php
Except, within this framework there are some directories from which files may be served without having the request to be handled by PHP. However, I don’t want to use the actual path to these directories, because I want to be able to have these paths interpreted by the PHP router.
Therefore I’d like to have aliases for these directories. For example:
RewriteRule ^i/com/(\w+)/(.*)$ framework/components/$1/images/$2
Now the problem is, that both of these rewrite rules work individually, but when I try to combine them, things go wrong. It looks like it reevaluates my entire .htaccess after the alias rewrite has occurred. Here are some examples of what I tried:
Skipping the rewrite-everything line under certain conditions:
rewriteRule ^i - [S=1]
RewriteRule .* framework/init.php
RewriteRule ^i/com/(\w+)/(.*)$ framework/components/$1/images/$2
Does not work. It appears like the second line gets skipped, then the third line rewrites to a condition where the second line does not get skipped, so then the second line executes.
Using an exclusive rewrite-everything line:
RewriteRule (?!i).* framework/init.php
RewriteRule ^i/com/(\w+)/(.*)$ framework/components/$1/images/$2
Same story.
Stopping the rewrite engine after the exception line:
RewriteRule ^i/com/(\w+)/(.*)$ framework/components/$1/images/$2 [L]
RewriteRule .* framework/init.php
Not working.
Adding a rewrite condition:
RewriteCond %{REQUEST_URI} ^(?!i)
RewriteRule .* framework/init.php
RewriteRule ^i/com/(\w+)/(.*)$ framework/components/$1/images/$2
Note that all of the above examples start working when I remove one of two RewriteRules. I’ve looked for similar questions, but they all have solutions that I tried to apply. The one difference between their questions and mine, is that I want to rewrite my “special” directory to a directory that, when normally requested, would result in a rewrite to init.php. Can anyone tell me if this is at all possible, and if so, what I am missing or not getting?
I found a solution today. I set an environment variable when the
^i-rule is matched, and check if that environment variable is set later. If it is, I skip the.*-rule. Like so:It might be a little ugly. But it works like a charm in older versions of Apache.