110

I am creating a website on localhost. I want to make all of link resources in my website to relative path ( I mean only internal resources).

The website is located in:

http://localhost/mywebsite

I read this useful question Absolute vs relative URLs.

I found differences between /images/example.png and images/example.png.

<a href="/images/example.png">Link To Image</a>

Above relative path should return ROOT_DOCUMENT/images/example.png because of / at the beginning of the URL. As ROOT_DOCUMENT is something like /wamp/www/mywebsite.

But when I tested it, it only returns /wamp/www/images/example.png.

And I should add manually my website folder /mywebsite/images/example.png to relative path.

<a href="mywebsite/images/example.png">Link To Image</a>

And it is not useful because of changing the name of mywebsite. So knowing that:

  • Why does this problem occur?
  • How can I resolve this problem?
Giacomo1968
25.4k11 gold badges78 silver badges106 bronze badges
asked Jun 4, 2014 at 3:34
4
  • 1
    This happens to me all the time when I use wampserver. Not an expert on how Apache/Web server works, but the idea is that the server operates over /var/www/ (its root) and your site is a subfolder within it. so if you put the / to make it absolute URL, then it takes the root that is /var/www/ (no subfolders) and you still need to add the folder where your web application is. Commented Jun 4, 2014 at 3:41
  • Put here virtualhost block of your apache config. Commented Jun 4, 2014 at 3:42
  • 1
    If you're starting your href's with / then that's not a relative path...that's site-root. Commented Jun 4, 2014 at 3:44
  • I believe that any answer requires percent-encodement of each path component. Commented Jul 18, 2025 at 10:58

3 Answers 3

179

You say your website is in http://localhost/mywebsite, and let's say that your image is inside a subfolder named pictures/:

Absolute path

If you use an absolute path, / would point to the root of the site, not the root of the document: localhost in your case. That's why you need to specify your document's folder in order to access the pictures folder:

"/mywebsite/pictures/picture.png"

And it would be the same as:

"http://localhost/mywebsite/pictures/picture.png"

Relative path

A relative path is always relative to the root of the document, so if your html is at the same level of the directory, you'd need to start the path directly with your picture's directory name:

"pictures/picture.png"

But there are other perks with relative paths:

dot-slash (./)

Dot (.) points to the same directory and the slash (/) gives access to it:

So this:

"pictures/picture.png"

Would be the same as this:

"./pictures/picture.png"

Double-dot-slash (../)

In this case, a double dot (..) points to the upper directory and likewise, the slash (/) gives you access to it. So if you wanted to access a picture that is on a directory one level above of the current directory your document is, your URL would look like this:

"../picture.png"

You can play around with them as much as you want, a little example would be this:

Let's say you're on directory A, and you want to access directory X.

- root
 |- a
 |- A
 |- b
 |- x
 |- X

Your URL would look either:

Absolute path

"/x/X/picture.png"

Or:

Relative path

"./../x/X/picture.png"
answered Jun 4, 2014 at 4:04
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, So If I transfer my codes to a host then this line refers to root of document "/pictures/picture.png" & everything works true ?
Note that the behaviour of the relative path can be changed via the <base> tag (developer.mozilla.org/en-US/docs/Web/HTML/Element/base)
shouldn't the relative path be ../../x/X/picture.png ? based on the example directory structure you need to move up two levels to get to root before moving back down, unless I am missing something.
82

The easiest way to solve this in pure HTML is to use the <base href="..."> element like so:

<base href="http://localhost/mywebsite/" />

Then all of the URLs in your HTML can just be this:

<a href="images/example.png">Link To Image</a>

Just change the <base href="..."> to match your server. The rest of the HTML paths will just fall in line and will be appended to that.

answered Jun 4, 2014 at 3:45

Comments

3

The relative pathing is based on the document level of the client side i.e. the URL level of the document as seen in the browser.

If the URL of your website is: http://www.example.com/mywebsite/ then starting at the root level starts above the "mywebsite" folder path.

Giacomo1968
25.4k11 gold badges78 silver badges106 bronze badges
answered Jun 4, 2014 at 3:41

1 Comment

This is poorly worded.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.