By tkhara
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.
https://www.evernote.com/l/AlnOgLBaJ7lBIYAequqvVe_VRMF7KH5Ue_U/
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.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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:
/full/doc/path/globe_bank/public/public/images/gbi_logo.png
You’ll need to change the path in your HTML relative to the doc root.
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…
https://www.evernote.com/l/Alnr0bIh0rVNvas_xmBD59R6VskfjaXLn4M/
Basically, the relative link works if I use something like
/var/www/phpmysqlcms.tk/html/globe_bank
as document root (and then generate the relative link)
But, it does not work when that root is changed to
/var/www/phpmysqlcms.tk/html/globe_bank/public
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.
Thanks.
If you use /var/www/phpmysqlcms.tk/html/globe_bank/public 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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.