1

I have re-written this entire post as I realized it originally did not make much sense (That would be a funny comment for a program).


I'm developing a web project in localhost with a very organized and deeply nested directory structure.

My current project directory:

# Absolute path
/Users/Username/Sites/myproject/
# Localhost path
localhost/~Username/myproject/

With PHP files, I can specify includes using the absolute path:

include_once '/Users/Username/Sites/myproject/config/scripts/php/foo.php';

However, with HTML anchor tags, CSS href, and JavaScript src, the absolute path cannot be used. For example:

The following anchor tag using an absolute path to the file:

<a href="/Users/Username/Sites/myproject/blog/">Blog</a>

Clicking the link causes a 404 error:

The requested URL /Users/Username/Sites/myproject/blog/ was not found on this server.

With the following example, looking at the URL on the 404 page, I can see that the server attempted to look for the file under the localhost root directory, rather than my machine's root directory:

[http://localhost/Users/Username/Sites/myproject/blog/]

Easy enough, I'll just point the file using an absolute path in the localhost directory:

<a href="/~Username/myproject/blog/">Blog</a>

Ah! A solution! I actually discovered this solution while making this edit to the post and, though it will work in all my directories no matter how nested, it's going to cause some serious problems down the line. First of all, this method does not work for PHP includes, PHP starts its file searching from the root of the machine, not the web server. This requires absolute paths that start all the way at the /User level, which will need to be changed when I upload this to me 3rd party host. I can think of another, much worse case, where I upload all the files to a 3rd party host, and have to go through and change all the links, to something like:

<a href="/blog/">Blog</a>

Since myproject will be the root folder (www.myproject.com). Sure, changing a few href, src, and php include locations won't be too bad, but I'm going to have to change every path on every file, which will be extremely time consuming and, based on how large this project is, close to impossible. And then when the nightmare comes of having to edit a file or two, change some CSS, etc, I'll download the related files via FTP and then I will have to change all the paths again to work on my local machine. This would be a nightmare.

So, my question is, how can I configure the OSX Apache web server to recognize myproject as the root folder in the sites directory? Is is as simple as editing DocumentRoot in httpd.conf? I'm hesitant to edit that file because if I mess anything up I'm screwed. Even if it is as simple as changing the DocumentRoot, what if I'm working on multiple projects at once? I'm not going to want to go in every time and change DocumentRoot just to get my paths to work. I think it would be pretty cool for each top-level directory in the Sites folder to be its own root folder for any files or directories inside of it, if that makes any sense. Anyway, any advice would help, I'm kind of dead in the water right now.

  • Why not simply upload the directory onto a web server? That way, the third party could access the files directly, using stable URLs rather than attempting to access those files from your local machine. – njboot Jun 20 '14 at 8:07
  • Well, it becomes a pain using FTP from the command line 24/7 every time I make a minor change to a file. Using the slow graphical interface is even worse. Basically it's much more convenient to develop on localhost – samrap Jun 20 '14 at 8:29
  • 1
    Okay, got it. Well, I can't provide you with a solution off-hand, but the reason why the tilde ~ doesn't work as a path identifier in javascript is because it's not considered a metacharacter in JS syntax, unlike in bash. – njboot Jun 20 '14 at 8:43
  • Well that's certainly interesting. At least that gives me an idea of why this isn't working. – samrap Jun 20 '14 at 9:00

You must log in to answer this question.

Browse other questions tagged .