Tags

, ,


Updated: 2016-02-16

This little snippet was developed when I built a web site to hold date stamped data files. The files are stored based on collection date so the format for the storage is  /data/YYYY/MM/DD/<data-file>.

Rather than using the default Apache indexing system, I wanted to use a custom PHP app developed using a Bootstrap GUI and I did not want to dynamically put an index.php file into every directory to achieve that.

The solution turns out to be rather simple, place 1 index.php file in the document root (in this odd case its in /data) and if a URI fetch results in a call to a path with no index.php, force the one in the root to be executed.

Changes do need to be made to the  index.php to collect the URI. This snippet of code below collects the URI, we then need to postfix it to the actual file system root path to get the file system path to the required directory.

function get_path()
{
$uripath = htmlspecialchars($_SERVER[‘REQUEST_URI’]);
if($uripath == “/”)
$dir=”/data/”;
else
$dir=”/data”.$uripath;
chdir ( $dir );
…. do something……
?>

Also note that in my application there are no query parameters after the path. If you need to process query parameters for any reason then you need to strip them from the URI from the “?” onwards.

For security, sanitize the path carefully to avoid traversals back into the file system below /data.

The vhost file for the defined web site (located in /etc/httpd/vhosts.d/) is pulled in by the Apache httpd.conf file with an Include (along with dozens of others).

Below is the site config file. Its located in:

/etc/httpd/vhost.d/vhost-datadump.test.com.conf

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
serverName datadump.test.com
DocumentRoot /data
<Directory /data>
Options +Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?index.php$
RewriteRule . /index.php
</Directory>
</VirtualHost>

This could also be used if your trying to AutoLoad PHP classes in a large application, I have not tried it yet but logically it should work.

-oOo-

Advertisements