I need to have multiple local sites in apache. So I add a virtual host for each one:

        ServerAdmin webmaster@example.com
        ServerName  www.example.com
        ServerAlias example.com

        # Indexes + Directory Root.
        DirectoryIndex index.php
        DocumentRoot /var/www/example.com/htdocs/

        # CGI Directory
        ScriptAlias /cgi-bin/ /var/www/example.com/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI

        # Logfiles
        ErrorLog  /var/www/example.com/logs/error.log
        CustomLog /var/www/example.com/logs/access.log combined

And then I do sudo a2ensite example.com and sudo service apache2 restart. The problem is that root-relative paths don't work. For example when I say <link href="/css/somefile.css" rel="stylesheet"> somewhere in /var/www/example.com/htdocs/index.php, apache fails to serve /var/www/example.com/htdocs/css/somefile.css to browser (browsing: localhost/example.com/htdocs)

What I'm missing?

  • That looks good to me, did you make sure that the Apache process would have permission to access the .css file? Dec 4, 2013 at 22:11
  • @AlexisWilke Since it works with relative path, I guess yes apache can see the file.
    – sorush-r
    Dec 4, 2013 at 22:21
  • Why do you browse to localhost/example.com/htdocs it should be example.com only Dec 4, 2013 at 23:26
  • @rechengehirn example.com lists htdocs, cgi-bin, etc.
    – sorush-r
    Dec 5, 2013 at 6:37
  • If you're testing multiple sites in a non production environment, you need to set up some fake DNS entries. The browser should tell apache what domain for which it expects to get a response. Just what I think the problem probably is. Dec 5, 2013 at 8:11

2 Answers 2


I think the problem is that you access the site with localhost/example.com/htdocs

You can do the following:

In your vhost configuration switch ServerName and ServerAlias:

ServerName  example.com
ServerAlias www.example.com

restart apache2 with:

sudo /etc/init.d/apache2 restart

Now in /etc/hosts add the 2 lines: example.com www.example.com

You sould now be able to access the site with www.example.com or example.com. (This works if you are working on the same machine where you have your apache installed)

And your path should work as expected.

Please comment, if you have any problems.

  • That is what I was missing all the time :)
    – sorush-r
    Dec 5, 2013 at 11:06

Ah! I think I understand your problem...

The link tag can use a full path, but the full path starts at the document root:

<link href="/css/somefile.css" .../>

The full path on disk (/var/www/example.com/htdocs/css/somefile.css) would not work indeed.

What Apache does is this:

DocumentRoot /var/www/example.com/htdocs/
<link href="/css/somefile.css" .../>

The DocumentRoot is some kind of virtual root (/) directory as far as the website is concerned. This prevents your visitors from accessing files they should never have access to (think of a file such as: /etc/passwd)

  • I like apache to do so. but it doesn't!
    – sorush-r
    Dec 5, 2013 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.