Mod_rewrite in multiple htaccess working together
I have a local intranet using mod_rewrite, which has a framework for running custom apps, which also needs mod_rewrite in some cases, but I can't get the combination to play together.
I want a particular application to have its own .htaccess
url rewriting without worrying about rewriting the framework that needs to be done. Therefore, the application has to rewrite part of the URL and get the framework from it.
Code first:
Infrastructure application .htaccess
in /intranet/apps
:
RewriteEngine On
RewriteRule ^([^\/]+)\/([^\/.]+)\/\?(.+)$ $1/index.php?screen=$2 [QSA,NC,L]
RewriteRule ^([^\/]+)\/([^\/.]+)\/?$ $1/index.php?screen=$2 [QSA,NC,L]
Application-specific .htaccess
in /intranet/apps/myapp
:
RewriteEngine On
RewriteOptions inherit
RewriteRule ^export\/([0-9]+)\/?$ export/?pid=$1 [QSA,NC,L]
This app has an export function that accepts a specific ID for processing. So, to break this down:
Url :www.intranet.com/apps/myapp/export/99
should be rewritten to
Url :www.intranet.com/apps/myapp/export/?pid=99
using the app .htaccess
. The structure should now take over:
Url :www.intranet.com/apps/myapp/export/?pid=99
to
Url :www.intranet.com/apps/myapp/index.php?screen=export&pid=99
My apps are working very well, so the framework does the rewrite as it should, but after the app re-release comes into play this page breaks and discards the 404
As I understand it, this script handles application-specific rewrites first, and the RewriteOptions inherit
parent rewriter is handled as follows.
The disgusting part is that for RewriteEngine on
an application, .htaccess
you just need to break it down into bits. I am obviously missing something, but I cannot handle it.
source to share
The problem is likely that inherited rules won't work in context myapp
. For the inside, myapp
you need a regex ^([^\/.]+)\/?$
because the URI that applies to those rules will be export/
, not myapp/export/
(because the rules are inside myapp
).
So add this to your framework app htaccess file (at the end of the file) or to your file myapp
:
RewriteCond %{REQUEST_FILENANE} !-f
RewriteCond %{REQUEST_FILENANE} !-d
RewriteRule ^([^\/.]+)\/?$ index.php?screen=$1 [QSA,L]
source to share