Reputation: 4632
Is it possible to make a relative path to root which would be equally valid from all directories?
I have some documents on the different dir levels, and my relative paths become invalid, if loaded from some other directory.
So if I store my text/html in the db with relative paths they simply won't work on some pages.
Example of a relative path:
../imgs/my_image.png
Then, if I store full paths as I develop my site, each time I move to new direction, or to an actual production domain from a localhost, all the embedded images (that come from db as text/html) wouldn't work either.
Example of an absolute path:
127.0.0.1/imgs/my_image.png
So the question is, is it possible without specifying the exact host name, like... pseudocode: %root%/imgs/my_image.png
or otherwise some other ways of solving it?
(I mean, without additional server processing of these paths, like using a regex replacing the paths, or creating additional tables to store my pictures, and without moving all the pages into one directory, like happens working with relative paths on CMS sites.)
It honestly bothers me, and I couldn't think of anything other than additional processing and updating of the records each time it's needed. But I would like to contribute something I discovered just accidentally.
Even though it did work in modern browsers like Chrome and IE, I totally doubt that this is a correct code. However if I used the highest level relative path to root, it happened to work for lower levels as well.
For example, suppose we have a page like this 127.0.0.1/auth/profile/content/index.php
and our image path is 127.0.0.1/imgs/my_image.png
, now the following code also worked for the pages on lower levels, e.g. 127.0.0.1/auth/prof.php
, and even on the root page itself 127.0.0.1/index.php
:
../../../my_image.png
Though logically there is no lower levels on the page in root folder. So I would also like to know some more about it, whether it's correct or erroneous, and whether we should use such in production...
Upvotes: 1
Views: 862
Reputation: 30453
Just use /
. It works fine and obvious.
For example, if you want to use images in your css and there is a folder called images
in the root of application, then you just use url(/images/image.png)
.
Upvotes: 1
Reputation: 10675
To point to the root of the website from any page under that domain just use /
So
http://www.example.com/pages/page.html
-> /pages/page.html
http://www.example.com
-> /
http://www.example.com/a/b/c/d/e/f.png
-> /a/b/c/d/e/f.png
Upvotes: 2