As can be seen in the StackOverflow post I made
.htaccess rules not routing properly on server (real file check),
I was having troubling getting my .htaccess
rules to route existing files
properly, while everything else to a singleton file. The following is how I
fixed it by removing mod_rewrite routing from the .htaccess
files, and
throwing them in my apache config file.
As a sample, I had the following file/folder structure:
.
├── [drwxr-xr-x] application
│ └── [-rw-r--r--] test.html
├── [drwxr-xr-x] core
│ ├── [-rw-r--r--] index.php
│ └── [drwxr-xr-x] tmp
│ ├── [-rw-r--r--] httpd-access.log
│ ├── [-rw-r--r--] httpd-error.log
│ └── [-rw-r--r--] rewrite.log
└── [-rw-r--r--] .htaccess
3 directories, 6 files
I was trying to get the following to happen:
Requesting /test.
html would serve the static file in /application/test.html
Anything and everything else (including the request
url.com/application/test.html
) would be routed to the core/index.php
file.
These were the rules I had set up:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/application%{REQUEST_URI} !-f
RewriteRule ^(.*)$ core/index.php?f=%{DOCUMENT_ROOT}/application%{REQUEST_URI} [QSA,L]
RewriteRule (.*) application/$1 [L]
No dice. /test.html
was being ignored by the first rule, and captured by the
second, as I'd hoped, but then in the subsequent sub-request (since the rules
were in a directory), it was being re-captured by the first rule.
My way around this? Get rid of per-directory routing. I through the following in my vhost:
# rewrites
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/application%{REQUEST_URI} !-f
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/core/index.php [L]
RewriteRule (.*) %{DOCUMENT_ROOT}/application$1 [L]
And boom! And while this isn't as flexible (eg. you had to modify your apache-config and restart your server), it works like a dream, and is ideally faster since there aren't per-directory rules/routes being processed.
If I find a way around this via the stackoverflow thread, I'll update here, but hopefully this can help someone.