Forum Moderators: phranque

Message Too Old, No Replies

Can Apache emulate unix "soft links"?

         

jrotering

9:46 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



I am hoping to find a way to configure Apache to treat requests for /{anywhere-deeper-than-the-root-directory}/myfile.php

as though myfile.php was there and was a "soft link" to
/myfile.php (in the root directory). In other words, I want to keep one copy of myfile.php in the root directory, and have any other requests for myfile.php from other subdirectories reference the one copy.

I need it to behave like a soft link, though, because the PHP file in question references some other files in the current working directory - which should result in different output depending on which directory the file is in. I tried using a basic RewriteRule statement, but that just serves the copy from the root directory as though root is the current working directory.

Does this make sense? Can anyone help me out with how I might do this?

Thanks in advance.

jdMorgan

10:59 pm on Mar 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> because the PHP file in question references some other files in the current working directory

How does it reference them, as HTTP-requested objects, or as <included> elements?

Jim

jrotering

11:08 pm on Mar 14, 2006 (gmt 0)

10+ Year Member



My PHP file reads in a javascript file in the current working directory which it assumes to be there (the whole point of the php file is to parse that js file and extract some values:

$file = 'siteInfo.js';

if (is_readable($file) == false) {
die("$file doesn't exist or we can't read it!");
}

$subdata = file_get_contents($file);

Additionally, the PHP file looks for subdirectories which also contain a 'siteInfo.js' file, and then links to a "links.php" file in each of those subdirectories. (Which is supposed to be a soft link of this same php file... kind of a recursive thing... make sense?)

$output = shell_exec("ls -l */siteInfo.js");
$match = preg_match_all("/\S*\//", $output, $matches);

foreach ($matches[0] as $subdir) {
echo "<a href=\"${subdir}links.php\">${subdir}links.php</a> <br />";
}

Birdman

12:00 am on Mar 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I were you, I would use PHP to find the relative path. Just add something like this to your local script before you include myfile.php:

$last_slash_pos = strrpos( $_SERVER['SCRIPT_NAME'], '/' ) +1);
$rel_path = substr($_SERVER['SCRIPT_NAME'], 0, $last_slash_pos);

Then, you can just add the $rel_path variable to you links in your script:

echo '<a href=' . $rel_path . 'your_soft_link.php';

jrotering

2:39 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



The problem is that by configuring a RewriteRule in httpd.conf that redirects /*/myfile.php to /myfile.php, the SCRIPT_NAME value always points to /myfile.php - the original path information is lost.

I figured out a solution; somewhat of a workaround but it is doing the trick. In the RewriteRule statement you can do a pattern capture - I am capturing the actual path, then appending it as a query string. So /subdir1/subdir2/myfile.php gets redirected to /myfile.php?PATH=/subdir1/subdir2. I had to modify my php script to pick up the $_GET['PATH'] value and prepend it to the links that the page builds. It is kind of kludgey but so far it is working.

jdMorgan

1:10 am on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is an external redirect necessary for your purposes? mod_rewrite can also do an internal rewrite, in which case most of the the original request parameters will remain available.

Jim