views:

149

answers:

2

Hello,

I am new at this so bare with me. I have just configured Apache and PHP to work on my local Mac OS X computer. Now PHP works fine, except when I try to load the files for my live sites. The live sites have separate directories and are sorted by client name etc.

I've created symlinks in the default root for the local web server documents. My issue is that Apache doesn't seem to want to load any of the relative paths that are found in the HTML pages. For example, I have src="/css/main.css" but Apache doesn't load the file, similarly for images, it just resolves as a file not found 404 error. I then thought it might be the symlinks so I copied the full directory into the Apache document root, and still had the same result.

I would really love to setup my local development environment to run Apache, PHP, MySQL to develop locally then publish when ready. I also tried the MAMP installation, and had the same issues.

Any help at all in this would be greatly appreciated. If my explanation wasn't clear please let me know.

Thanks! Alex.

+1  A: 

Have you tried src="css/main.css"? That is, without the leading slash? If you have a leading slash there your files would have to be in a directory named css that was in the root directory of the webserver, and if I understand you correctly that's not the case.

EDIT: OK, from reading your comments it seems like you are not quite clear on how relative urls work. "/css/main.css" is not relative to the page's location in the directory tree. It means a file named "main.css" in a directory named "css" in the root directory of the webserver. When you put your files on the deployment server your css directory is at the webserver's root directory. But it sounds like you are currently putting the css directory in a subdirectory named for the client... so your css file is now living at "/clientname/css/main.css".

If I understand you correctly, you can do what you want by using relative urls. If your html file is in the same directory as the css directory you would need "css/main.css". If it's in a subdirectory of the directory that contains the css directory you would need "../css/main.css"- the ".." means the parent directory of the current directory. If you use relative urls they will continue to work as long as the relationship between the files doesn't change.

Here's a page on the subject that explains it adequately, I think: http://www.webreference.com/html/tutorial2/3.html. Was pretty much the first thing I found in Google though, so there are likely better explanations out there.

There are a number of Apache directives that you could use to do this, but if using relative urls would work for you (and if I understand you correctly it would) then that's likely to be a lot simpler and less likely to cause you further trouble.

T Duncan Smith
Well my issue is that I want the local web server files, and the live web server files to be identical. That is, I'd like to just make the changes in my local files, save, test locally, then publish from there. I usually add the /css/main.css because I create separate header files which are imported across the site in different folders. Perhaps there is a way to tell Apache to fill in the gap? like /path_to_document_root/css/main/css ?Thanks!
Alex E.
Perhaps a better question is, why does /css/main.css work properly on the live web server, but not on my local one?
Alex E.
A: 

First you might want to try using src="./css/main.css".

When dealing with multiple live sites I like to setup a single configuration file for each site with apache and then load them all together in the httpd.conf file.

for my setup it looks like this:

in /etc/apache2/httpd.conf

I have:

# Begin virtual host directives.

Include conf/bortreb.conf

Include conf/rlmcintyre.conf

Include conf/laserkard.conf

Include conf/judyates.conf

and then in /etc/apache2/conf/judyates.conf

I have:

    <VirtualHost *:80>
    #localhost site

    ServerAdmin [email protected]
    DocumentRoot "/home/r/Desktop/web/judyates"
    ServerName localhost
    ServerAlias judyates.localhost
    ErrorLog "/home/r/Desktop/web/judyates/log/error_log.log"
    ScriptAlias /cgi-bin/ "/home/r/Desktop/web/judyates/cgi-bin/"

    <Directory "/home/r/Desktop/web/judyates">
        Options Indexes FollowSymLinks
        Options +ExecCGI
        AddHandler cgi-script cgi pl py
        AllowOverride Options
        Order allow,deny
        Allow from all
    </Directory>

    </VirtualHost>


  <VirtualHost *:80>

    #live site
    ServerAdmin [email protected]
    DocumentRoot "/home/r/Desktop/web/judyates"
    ServerName judyates.com
    ServerAlias *.judyates.com
    ErrorLog "/home/r/Desktop/web/judyates/log/error_log.log"
    ScriptAlias /cgi-bin/ "/home/r/Desktop/web/judyates/cgi-bin/"


    <Directory "/home/r/Desktop/web/judyates">
        Options Indexes FollowSymLinks
        Options +ExecCGI
        AddHandler cgi-script cgi pl py
        AllowOverride Options
        Order allow,deny
        Allow from all

    </Directory>

    </VirtualHost>

This way works really well, because you can set the subdomain yoursite.localhost to loop back to your home ip address.

With this setup, when I work on judyates.com on my computer and want to test anythig, I just go to judyates.localhost in my web browser.

I have about 5 other sites all set up this way in their own *.conf file, so they can each live in their own directories on my computer that exactly match the directories they'll be in on the server.

The key is to use virtual hosts to go to different sites based on the subdomain.

You can learn how to configure subdomains that point to yourself here: http://digitalpbk.blogspot.com/2007/01/making-subdomains-on-localhost.html

My setup goes even one step further because I setup the server too. Whenever I want to update I load both the webfiles AND the apache config files, and that way the server exactly mirrors my local setup. The only difference is that the real judyates.com points to the server and not my home computer, so when people try to visit the site they get everything from the server.

Robert McIntyre