I do this a lot on my own sites to separate 3rd-party applications and various other things from my core code base that I keep in subversion.
The way you've tried to do this via the ../ syntax doesn't work because of security restrictions. You don't want people to be able to access any arbitrary file on your web server. You're limited to DocumentRoot for your own protection, otherwise script kiddies would be able to get things like usernames and passwords that you keep in plain text config files, or worse, customer data from your databases. Some years ago I recall a vulnerability that allowed that to work, which led to some pretty nasty exploits.
That said, it can be done. You just have to deploy a little server-side magic.
You haven't mentioned what web server you're using. If you're using Apache, you can use a nifty module called mod_rewrite.
First you need to identify what you want to redirect to a folder outside of DocumentRoot. Let's say you have a folder called /Applications that lives parallel to DocumentRoot, as a sibling folder.
Set-up a rule in httpd.conf, or in a .htaccess file that looks like this:
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# If the REQUEST_FILENAME does not exist as a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php [L]
</IfModule>
I use this exact configuration in a .htaccess file. It redirects every request for a file or directory that doesn't exist in the file system to my index.php script. In the index.php script I can use PHP to parse the path and output file contents. I use this for every file on my website, which lets me have some files that are database-stored, and some that are actually in the server's file system... essentially I create my own file system. That makes it easier to control what goes into subversion, it makes my paths
SEO friendly. On the outside my website appears to be completely static.
Of course, you don't have to make things so complicated, you can also use this script to reroute certain directories, so you can store them outside of your DocumentRoot. At the same time though, you'll also have to make your own 404 error handling.. since the mod_rewrite configuration redirects everything that doesn't exist.
So, I recommend exploring that. You don't have to call your script index.php, by the way, you can make it whatever you like, just update the path in mod_rewrite accordingly.
In your script, you want to parse the path with parse_url(), be sure to escape quotes though, cause that will happily leave you vulnerable to SQL injection vulnerabilities. Then, all you have to do is use PHP's file and directory handling functions to grab and output the right files. You'll have to set the Content-Type HTTP header manually...
So, if you have the path...
http://www.example.com/Applications/something.js
You do this in your script...
Code:
<?php
$uri = parse_url($_SERVER['REQUEST_URI']);
$mime = '';
$php = false;
switch (true)
{
case strstr($uri['path'], '.css'):
{
$mime = 'text/css';
break;
}
case strstr($uri['path'], '.js'):
{
$mime = 'application/javascript';
break;
}
case strstr($uri['path'], '.html'):
case strstr($uri['path'], '.htm'):
{
$mime = 'text/html';
break;
}
case strstr($uri['path'], '.jpg'):
{
$mime = 'image/jpeg';
break;
}
case strstr($uri['path'], '.png'):
{
$mime = 'image/png';
break;
}
case strstr($uri['path'], '.gif'):
{
$mime = 'image/gif';
break;
}
case strstr($uri['path'], '.php'):
{
$php = true;
break;
}
default:
{
echo "Unsupported file type.";
}
}
$absolutePath = '/absolute/path/to/file'.$uri['path'];
if (file_exists($absolutePath))
{
// If /absolute/path/to/file/Applications/something.js exists
if (!$php)
{
header('Content-Type: '.$mime);
echo file_get_contents($absolutePath);
}
else
{
include $absolutePath;
}
}
else
{
// Write your custom 404 script here...
}
?>
Or something along those lines... I've even used this to compress my css and javascript by stripping out excess whitespace, comments, and line breaks.
Hope this helps!
Richard
--
http://www.deadmarshes.com
Author, Various Wrox Books