Routing all sub-directory Drupal site requests through an upper-level PHP file?

(I filed this under the “CMS & Wordpress” category due to involving Drupal but maybe it should be somewhere else?)

Here’s the issue:

I’m trying to route all sub-directory Drupal site request traffic through a single PHP file prior to visitors reaching the respective websites they try to visit. The intention being to have a singular point where I can evaluate traffic better instead of being forced to implement custom approaches in each Drupal installation.

So for example, if I have Drupal installed at “www/mydrupal,” and visitors can access this by going to “http||https://(www.)mydrupal.com,” I want to force all requests for this site to go through something like “www/index.php” so that I can have a chance to evaluate the traffic.

That being said, I use Drupal a lot for many projects and since I only have 1 account with my web host, I have to make use of sub-directories for each Drupal installation I maintain, which is okay until I wish to do something more global like this.

In a bigger example, let’s say I have the following folders in WWW:

Is it possible to create an index.php file at “www/index.php,” and force all requests to be evaluated by this file before being subsequently routed onward to whatever site the visitor was initially requesting? With the URL being respectively rewritten to hide the sub-directory? If so, how?

Unless I’m mistaken, I think this means that I need to basically tell my host to reassign all alias I have to be directed to the 1 index.php file but then how would I route the requests to the respective sub-directory for Drupal to take over after the preceding file has a chance to process the requests?

Any insight into this would be appreciated.

So I think I found the solution for my situation… It all revolved around adding the following line of code to my .htaccess file, which makes the index.php file run before each page load:

php_value auto_prepend_file "../index.php"

(Or it can be added into the php.ini file, too–whatever works for your case.)

If using this in a global context throughout multiple Drupal deployments, the logic in the index file has to be pretty abstract or else you have to be sure to add filtering logic to denote which site is being evaluated (which can get hairy).

Anyway, just thought I’d post this here in case anyone else is trying to achieve the same result. Found this solution from Stack Exchange.

Edit: forgot to mention that I also had to use the $base_url variable in the settings.php file.