In my website, I have an html file inside of a folder, that is next to the index file. I want to add a link back to the index file.
For example, the URL for the index would be, example.com. The 2nd page is example.com/pages/example.html. How would i send the user back to the index page?
Assuming you have the following structure:
website.com/index.html
website.com/pages/example.html
You have three options -- two relative links, and one absolute link:
Relative to example.html:
Back to the index
Relative to the root (note the slash):
Back to the index
Absolute:
Back to the index
I'd recommend using a relative root, as your website URL may change in the future, so you don't really want to hardcode it.
As for which of the two relative approaches to use, relative to the root (/index.html) would be best in your base, as it will always take you back to the index -- you may move example.html to the same folder as index, or even a subfolder of pages, in which case the second relative link would need modification. If you have that link in multiple pages, that could prove annoying ;)
For further reading on relative and absolute URLs, I recommend checking out the Coffee Cup article.
Hope this helps! :)
Related
This question already has answers here:
Absolute vs Relative Links : Technical Difference
(2 answers)
Closed 4 years ago.
I have a question about absolute and relatives links as I am working on an assignment and seem to be a bit confused... what are the different situations that each type of link would be used in?
Thank you!
In short, relevant links look for files in that same folder structure. For example, if you had the following structure for your files:
root
/assets
/img
image.jpg
index.html
Then when finding an image to use on your image.html page, you could enter <img src="assets/img/image.jpg">. An absolute URL includes the full URL to that image, so it would be something like <img src="https://example.com/assets/img/image.jpg">.
Generally, using relative URLs is the easiest to manage things. If you use a content management system it will often use relative URLs.
Absolute URLs can be used everywhere - you'll need to use them if the URL isn't in your site structure.
Either one will get the same result if you have the image on your server, relative URLs are usually just faster to type. :)
#Danny Santoro does a good job explaining what relative and absolute links are. As for when to use them:
Use relative links whenever you're linking between pages on the same domain. For example, if you're working on https://www.google.com and you want to link to https://www.google.com/orange, you should use a relative link which would look like this: Go to Orange.
Use absolute links whenever you're linking between two pages on different domains. For example, if you're working on https://www.google.com and you want to link to https://www.facebook.com, use an absolute link: Go to Facebook.
As for why relative links are better if both the source and destination are on the same domain - let's say the site you're working has the domain oranges.com. Then you use absolute links - hard code every link on your site to be www.oranges.com/foo. Then later down the line, you want to switch your domain to be www.grapes.com. Now you have to go back and manually change every single link on your website to say www.grapes.com! Whereas if you'd just used relative links, you wouldn't have to change anything.
If you're linking to another domain, then you have no option but to use absolute links. Relative links only work when both the source and destination are on the same domain. So in this case you would use absolute links.
When I browse the source of websites, sometimes I see links like this:
...and sometimes I see links like this:
What is the difference between these two relative paths, if any, and why should I use one over the other?
The first is relative to the directory of the current resource. The second is relative to the root web directory.
If my path is "http://www.example.com/folder1/folder2/" how can I return to "http://www.example.com/folder1/"
I've tried
Back
Which get me back to "http://www.example.com/" and not the previous folder. Any clue on how to do it ?
EDIT
I have a
<base href="http://www.example.com/" />
in the head, could it be because it try to go down 1 folder from that base ?
Back
Should work if you drop the trailing slash. Don't really know why it wouldn't work with it but if it's not working, try that...
EDIT Just tested and both versions worked for me in IE8 - what browser are you using to test it?
Another Edit
If you use a <base> tag, all relative hrefs will be relative the href attribute specified as the <base> (thats the point of it). If your base is the root of the site, like http://www.example.com/, links that try to step down a directory won't make any sense, since there's nowhere lower to go, and they will just point to the root directory. However, if the base is the root of the site, there's probably no point in having one at all, since this is, effectively, the default - it would only make a difference if you were working from a higher level directory.
If the base is not the root, but somewhere higher (like http://www.example.com/somedirectory/) there is a point in the base declaration, but you have to make a decision - something would have to be specified absolutely. So if you have done it because you want to refer to all your images as just file.jpg instead of /somedirectory/file.jpg, you either need to change your image references to the absolute /somedirectory/file.jpg, or have your 'navigation links' like the one shown in the question specified absolutely. You can't do both.
I would say (although I don't know much about the rest of your site and how it was built) that your best bet is to scrap the <base> in order to allow for relative navigation links, but the choice is yours...
I got a question and although I could find related information, I'm if it was exactly about what I'm wondering about.
The thing is, I got a site on http://localhost/site.
Now, when I create a link, let's say, <a href="/posts">, it links to http://localhost/posts instead of http://localhost/site/posts.
It works fine if I remove the slash (<a href="posts">), that would be the closest and maybe the easiest solution, but I'd like to know why the links ignore the folder where the file is at?
And I also would like to know if this can be fixed with .htaccess or something.
I've read that a link that begins with / makes it "absolute". So a link beginning with / is only intended to be used to link directly to the root, or to be used by sites stored at the root (in this case it wouldn't make much sense?) ?
The leading '/' at the start of the URL informs the web browser that the path given is absolute (with respect to the web server root), i.e. if you link to /posts then you know that the resulting link will be to http://www.mysite.com/posts.
If you don't supply the leading '/' (and you don't give a complete url like http://www.mysite.com/posts) then usually the url is relative, and any page given will be relatvie to the page currently being viewed.
For example:
page being viewed link url target page
------------------------------------------------------------------------------
www.mysite.com/site link.html www.mysite.com/site/link.html
www.mysite.com/site ../link.html www.mysite.com/link.html
www.mysite.com/some/other/page link.html www.mysite.com/some/other/page/link.html
www.mysite.com/some/other/page ../../../link.html www.mysite.com/link.html
The decision on whether to use absolute or relative links is entirely up to you - the advantage of relative links is that if your site moves, links between pages on your site will still work correctly (for example if your site moves to www.mysite.com/otherpath, then any absolute links such www.mysite.com/originalpath/home will no longer work.
You should see the following site for a more complete explanation of relative urls:
Relative URLs (WebReference.com)
Your site root is localhost although you assume that site is your site root. When you use / it is relative to localhost as it is an absolute link.
Try doing it < a href="../posts" >
./ Means base directory, or home
../ Means one directory up
I have a website, let's call it example.com. Within this site, I have some FAQs but the person that built the site saved the FAQ pages under a directory on the site named "FAQs".
As an example an FAQ page would be located at:
example.com/pages/en/faqs/faq-page1.html.
Note the pages/en/ directory. Ideally I would like all the pages to be saved under example.com/index.html etc but I can't change this.
Anyway, when I am on any of these FAQ pages, and I try to link back to say the home page index.html the navigation won't go to the page. So for example, when I am on:
example.com/pages/en/faqs/faq-page1.html
and I try to link back to the home page
example.com/pages/en/index.html (which is where the index page is saved) the nav won't work. Instead it will try to go to example.com/pages/en/faqs/index.html.
Now I am assuming this happens because I am in the "faq" directory, but how do I go back to the root directory when linking? The code for the link is simply Home. I could of course just put in the full link example.com/pages/en/index.html, which would solve this but is there another way around this?
You need to give a relative file path of Home
Alternately you can specify a link from the root of your site with
Home
.. and . have special meanings in file paths, .. means up one directory and . means current directory.
so Home is the same as Home
There are two type of paths: absolute and relative. This is basically the same for files in your hard disc and directories in a URL.
Absolute paths start with a leading slash. They always point to the same location, no matter where you use them:
/pages/en/faqs/faq-page1.html
Relative paths are the rest (all that do not start with slash). The location they point to depends on where you are using them
index.html is:
/pages/en/faqs/index.html if called from /pages/en/faqs/faq-page1.html
/pages/index.html if called from /pages/example.html
etc.
There are also two special directory names: . and ..:
. means "current directory"
.. means "parent directory"
You can use them to build relative paths:
../index.html is /pages/en/index.html if called from /pages/en/faqs/faq-page1.html
../../index.html is /pages/index.html if called from /pages/en/faqs/faq-page1.html
Once you're familiar with the terms, it's easy to understand what it's failing and how to fix it. You have two options:
Use absolute paths
Fix your relative paths
To go up a directory in a link, use ... This means "go up one directory", so your link will look something like this:
Home