The Setup
My setup is to have resources shared for two or more sites that have similar structures though different content. In example…
http:// localhost/site1/
http:// localhost/site2/
The rewrite rules are intended to add exceptions for the shared resources. In example the scripts folder (and of course everything inside of it) is rewritten so…
http:// localhost/scripts/
…is accessible at…
http:// localhost/site1/scripts/
http:// localhost/site2/scripts/
The Problem
When trying to access…
http:// localhost/site1/scripts/admin.js
…the following rewrite applies via the current rule and tries to rewrite it to the admin directory…
RewriteEngine on
RewriteRule .*/admin(.+) admin$1 [QSA] #Messes with scripts/admin.js
I think this is a directory depth issue, where the rule .*/ can apply to more than the first directory (site1/) and also inherently (and undesirably) applies to all directory depths (e.g. depth1/depth2/depth3/ etc).
Desired Outcome
How do I adjust this rule so that…
- The admin index (site1/admin/) is caputed by this rule.
- The admin JavaScript file (site1/scripts/admin.js) is ignored by this rule.
- We keep the dynamic starting bit of the rule (RewriteRule .*/admin) so no matter the name of the directory that is used Apache will continue to automatically just work or we can modify it to have the same effect but the end result is that it must remain dynamic.
- I do not rename admin.js as it would be cheap and thus I won’t learn from this.
A couple of things you can try depending on how you have everything else working. If the site1/scripts/admin.js exists and you want to have URI’s that access existing files (like scripts, or images) not to be rewritten, you can try adding this right before the RewriteRule:
Or, you can also just make it so scripts won’t get rewritten by adding this right before the RewriteRule:
Requests that end with .js will fail that condition and the rule will not be applied. Alternatively, you can narrow it down specifically to
admin\.jsorscripts/[^\.]+\.js(replacing the\.jsin the condition).You can if you use
[^/]+instead of.*.