Skip to main content
martcol
Inspiring
March 6, 2010
Answered

Getting Confused with Absolute/Relative links

  • March 6, 2010
  • 1 reply
  • 1915 views

I have a simple site and am using PHP includes.

I'm using paths that have the ../ in front of them so they are relative.  I understand that I can use a path with just the fwd slash and not the two periods in front but I'm not sure why.

My site works ok live but not when I use localhost to preview in the browser.

What am I doing wrong or not seeing?

Thanks

Martin

This topic has been closed for replies.
Correct answer David_Powers

Moved to the Dreamweaver Application Development forum, which deals with PHP and other server-side things.

You're also confused about the difference between absolute and relative links. An absolute link is a fully qualified link - it contains the full path. Links that begin with ../ and / are both relative links. The first style is relative to the document. The second is relative to the site root.

When using PHP includes, the path to the include file must either be relative to the document or the file must be in the PHP include_path.

When using links inside an include file, they should normally be relative to the site root.

The reason your links are not working locally is because the site root is localhost. You are probably developing your site in a subfolder of localhost. Consequently, the links are pointing to one folder above what you want.

The solution is to create a virtual host. The gory details on setting up virtual hosts in Windows can be found in this tutorial on my site: http://foundationphp.com/tutorials/apache22_vhosts.php.

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
March 6, 2010

Moved to the Dreamweaver Application Development forum, which deals with PHP and other server-side things.

You're also confused about the difference between absolute and relative links. An absolute link is a fully qualified link - it contains the full path. Links that begin with ../ and / are both relative links. The first style is relative to the document. The second is relative to the site root.

When using PHP includes, the path to the include file must either be relative to the document or the file must be in the PHP include_path.

When using links inside an include file, they should normally be relative to the site root.

The reason your links are not working locally is because the site root is localhost. You are probably developing your site in a subfolder of localhost. Consequently, the links are pointing to one folder above what you want.

The solution is to create a virtual host. The gory details on setting up virtual hosts in Windows can be found in this tutorial on my site: http://foundationphp.com/tutorials/apache22_vhosts.php.

martcol
martcolAuthor
Inspiring
March 6, 2010

Thanks David,

Now you've explained it I think I knew it!

I've looked before at creating virtual hosts but it was a bit too much for me at that time.  I may be ready for another go now!

I'm presuming from your reply that I haven't done anything wrong as such but that this is a limitation of not using virtual hosts or using includes?

Thanks for pointing me in the right direction.

Martin

David_Powers
Inspiring
March 6, 2010

martcol wrote:

I've looked before at creating virtual hosts but it was a bit too much for me at that time.  I may be ready for another go now!

Creating virtual hosts is actually quite easy. It seems overwhelming the first time you try it, but after that it's very simple.

I'm presuming from your reply that I haven't done anything wrong as such but that this is a limitation of not using virtual hosts or using includes?

Well, of course, you might have done something else wrong, but it sounds as though it's just a question of using a subfolder of localhost with site-root-relative links.