Ganhammar
Ganhammar

Reputation: 1951

Requesting files from another parent directory

Lets say that we have a server running multiple virtual hosts. Somefiles and directories are shared amongst these virtual hosts, such as JS-, HTML-, and some PHP-files. I want to prevent having to make a new copy of these files every single time we add a new virtual host, since it would make it hard to keep the files up to date. So what I want to do is to put these shared files in another parent directory i.e. /var/www/shared_files/.
So when one of the shared files is requested i.e. http://example.com/scripts/gallery.js I want to get the file located in /var/www/shared_files/gallery.js instead of /var/www/xxx/gallery.js.

The first solution I found for this problem was a htaccess solution:

RewriteRule ^gallery.js ../shared_files/gallery.js

Which only works as long as all the files is requested via the servers IP, i.e. http://11.11.0.111/xxx/gallery.js.

The second solution is a PHP solution:

if(* is 404 *)
{
    if(file_exists("/var/www/shared_files/* the requested file name *"))
    {
        if(strpos(* the requested file name *, ".js") !== false)
            header("Content-type: text/javascript");

        require_once("/var/www/shared_files/* the requested file name *");
        die();
    }
}

This dosnt seem like a good solution in the long run so do anyone know of a better solution for this?

Upvotes: 1

Views: 93

Answers (1)

JochenJung
JochenJung

Reputation: 7213

Since your shared files reside in a location outside the VirtualHost, you should access them via an own domain/subdomain.

So you should link http://shared.example.com/gallery.js instead (where shared.example.com is a subdomain, that links to your shared files folder).

Upvotes: 1

Related Questions