Answer the question
In order to leave comments, you need to log in
Can a link to a file work without specifying the full path?
Goodnight! I am writing a website template. The site is located at bluehost.com. The template is in regular php, I took the layout from the layout designer. Naturally, out of habit, I began to change links, since the view is in the views folder, and all css and js files are in the neighboring assets folder. Here it worked right away: <link rel="stylesheet" href="css/all.css">
Corrected for this: <link rel="stylesheet" href="assets/css/all.css">
And it also works.
There is no css folder in the views folder. Moreover, there is no css folder a level higher, that is, next to the views folder. But there is an assets folder and a css folder in it, which contains the all.css file. But how does the web server know where the css folder is if no intermediate assets folder is specified? Here is the folder structure for more clarity:
/assets/css/all.css
/views/template.php
Answer the question
In order to leave comments, you need to log in
As @voiddancer and @nazarpc rightly wrote, it doesn't matter where you have the file on the disk. This is html. It is parsed by the browser on the client side. He knows the url by which the page was requested and knows nothing else.
Links with "/" at the beginning are loaded on behalf of the domain.
Links without - from the current path.
That is, if you have the page url mysite.ru/partners,
then "/css/all.css" will be searched for mysite.ru/css/all.css
and "css/all.css" - mysite.ru/partners/css/ all.css
Actually, all this can be easily viewed in the browser console.
If you have an exact understanding that the site will always live on a separate domain - write links like "/css/all.css".
If there is no such understanding, set a variable $basepath = 'http://mysite.ru'
in the site config and generate absolute links in php: $basepath . '/css/all.css'
I would not recommend using the base tag, because firstly it seems to me wrong to edit html layouts when changing the domain, and secondly, I have repeatedly encountered its strange behavior in various browsers.
relative links in web code work a little differently than they do in any axis file system. here the links start with the domain name, it is simply substituted for any relative link.
Or the paths can be considered relative to the href attribute of the base tag if one is available.
It is very strange that the first option worked, you need to see where the browser sends requests, whether there is a base tag , and the demo would dispel doubts.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question