Setting my project public folder as document root

I am trying to set the public folder in my project as the document root.

I have explained everything in detail at this link with screenshots.

When I set the root to one folder above public the index files picks up the css file. Otherwise it does not… Not sure what the problem is. Any guidance is appreciated. Thanks.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Thank you. I am all set for now. Thanks for your help!

If you use /var/www/ as your document root, then you don’t need any hack or function to build your links. This should work OK:

<img src="/images/gbi_logo.png" />

There’s no need to pull that info. Even $_SERVER['DOCUMENT_ROOT'] will give you the apache docroot but you don’t need that.

Perhaps I’m not fully understanding what you’re setting out to do.

Okay I found the issue with what I was doing as I was working on a response to your suggestion. In one way or another, you helped me solve this :)

Thank you.

P.S. I have posted at the following link what I was doing wrong:

Hi Unixynet,

Thank you for your response.

I am using php to set the URLs relative to the root.

This is the code for setting the stylesheet url:

<link rel="stylesheet" media="all" href="<?php echo url_for('/stylesheets/public.css'); ?>" />

And, here is the url_for function:

// Creates a url by appending the root to the script path sent in
// The root ends at public the script should be everything after that
  function url_for($script_path)
    // add the leading '/' if not present
    if ($script_path[0] != '/') {
      $script_path = "/" . $script_path;
    return WWW_ROOT . $script_path;

And here are the definitions for the WWW_ROOT

// Assign the root URL to a PHP constant
  // * Do not need to include the domain
  // * Use same document root as webserver
  // * Can set a hardcoded value:
  // define("WWW_ROOT", '/~kevinskoglund/globe_bank/public');
//   define("WWW_ROOT", '/public');
  // define("WWW_ROOT", '');
  // * Can dynamically find everything in URL up to "/public"
  $public_end = strpos($_SERVER['SCRIPT_NAME'], '/public') + 7;
  $doc_root = substr($_SERVER['SCRIPT_NAME'], 0, $public_end);
  define("WWW_ROOT", $doc_root);

So this function should find everything till /public and then the $script path argument is appended to it /stylesheets/public.css

Please see some more things I have tried to get the document root right so that the relative CSS link works…

Basically, the relative link works if I use something like


as document root (and then generate the relative link)

But, it does not work when that root is changed to


And I was thinking that having a separate public folder for a site would be for the purpose of setting it as the root so that people cannot type a URL to reach the private folder.

Any further guidance is appreciated.


The CSS and assets in general are looked up by the web server relative to the document root. So when you set the document root to globe_bank/public then this:

<img src="/public/images/gbi_logo.png" />

Gets translated to:


You’ll need to change the path in your HTML relative to the doc root.