Absolute and Relative Linking

He has: 5 posts

Joined: Jun 2004

I developed a site on a mac using a virtual host. I used links throughout with a leading slash to indicate a directory at the top level of the site on the local machine. So, a link referencing an image file is like this: /images/file.jpg (with images being inside the site folder along with index.html). This works just fine locally on my virtual host, but not on the remote server (a VPS). The files can't find the css, images, and ssi directories––it does work when I remove the slash. So, should it work on the remote server or does the slash tell the server to go all the way to the server's root directory as opposed to public_html?
Thanks,
John

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

The leading slash SHOULD tell it to go to the root of the domain (or ip address), the browser has no knowlege of the file structure of the server, so it can only go off of the domain.

If you have domain.com/images/file.jpg as where your files are, any file under domain.com that calls /image/file.jpg should access it fine.

However, if you have domain.com/username/images/file.jpg , and you consider your main site to be domain.com/username then you will have to change all your links to be /username/images/file.jpg This is the only thing I could think of that may be wrong, and many free hosts use a setup like this.

-Greg

He has: 5 posts

Joined: Jun 2004

Greg,
Thank you. This is a cpanel setup on a VPS that allows hosting multiple domains. All the domains on the VPS share the an IP and cpanel sorts out the calls based on actual domain or user. Another factor is that the domain I'm working on is not active so I'm using "IP/~user" to access the site. So I guess I need to put the site on an active domain to finish testing because even the relative links within public_html aren't working properly. But I do appreciate knowing that it "should" work and will continue to pursue it.
Thanks again,
John

Greg K;209092 wrote: The leading slash SHOULD tell it to go to the root of the domain (or ip address), the browser has no knowlege of the file structure of the server, so it can only go off of the domain.

If you have domain.com/images/file.jpg as where your files are, any file under domain.com that calls /image/file.jpg should access it fine.

However, if you have domain.com/username/images/file.jpg , and you consider your main site to be domain.com/username then you will have to change all your links to be /username/images/file.jpg This is the only thing I could think of that may be wrong, and many free hosts use a setup like this.

-Greg

He has: 5 posts

Joined: Jun 2004

OK, to follow up on this... there is no problem using links formatted with a leading slash when on a live domain. My problem was caused by using IP/~user to reach a cpanel site which has no live domain pointed to it. If each domain had its own IP this may not have been a problem.

He has: 1,758 posts

Joined: Jul 2002

Hi,

That will be why then...

If the url was:

http://www.yourdomain.com

/ would go to the root which is the domain,

However when using:

http://111.222.333.444/~yourdomain.com

The tilde (~) basically skips all the middle parts. Your actual path is probably something like:

http://111.222.333.444/home/sites/yourdomain.com/

So the / goes to the root of the IP which is of course, not where the files are.

Easiest solution is to edit your local hosts file (using netinfo manager I think) on your mac to make yourdomain.com resolve to the IP of your server until you can actually put the new site live. Then for you the site will resolve to the new server, but for everyone else the site will resolve to the old one.

This document should help you with configuring netinfo manager:

Andy

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.