Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Relative and absolute paths

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,512
Scotland
Suppose my site has a directory named RootDir. Below that, there's another directory named ArticleDir.

RootDir contains a file named doc.html, which I want to link to from various points in the site.

To reference the file from a page in RootDir, I would do this:

Code:
<a href="doc.html">Click here</a>

To reference it from a file in ArticleDir, I would have to go up one level:

Code:
<a href="[b][blue]../[/blue][/b]doc.html">Click here</a>

My question is: Is there any way of specifying an absolute path rather than the relative ones shown here? In other words, I want to reference the document in the same way regardless of which level I'm on.

I know I could do it by referencing the URL, but I'd like to find a way that would without having to know the final URL of the site.

I hope this makes sense.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Use absolute paths

If you start your path with a slash (/), it's the same as the specifying the domain name
Code:
/images/bigpic.jpg
is the same as
Code:
[URL unfurl="true"]http://yourdomain.com/images/bigpic.jpg[/URL]

Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
 
Thanks, Greg. I've just tried your suggestion, and the forward slash does indeed resolve to the domain name, just as you said.

But that raises another issue. I'm developing the site on my local hard drive. These are static HTML pages. I'm not running a local web server for development. I've simply set up a directory structure to reflect that of the eventual production server.

The problem is that, if a page contains:

/images/bigpic.jpg

then the forward slash is interpreted as the root of the drive, not the root of the site's directory structure. In other words, the path resolves as:

file:c:///images/bigpic.jpg

I can see why that's happening. And I can see that I can resolve it by placing the root files in the root of the drive (in C:\), and each sub-directory direcly off the root. But that would messy.

It's not a big problem. It just means that I can't click on these absolute links during development. But I wonder if there's an easy of way round it? Do other people have this issue, and, if so, how do they solve it?

Mike


In other words, I can

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
I can't click on these absolute links during development. But I wonder if there's an easy of way round it? Do other people have this issue, and, if so, how do they solve it?

I'd set up a web server for local development... Apache is fairly easy to use and free, and will help avoid this issue.

However, if you are only ever using relative paths, then you could put a BASE element in your HEAD section, e.g:

Code:
<base href="file:///C:/Some/Dir/">

This will be automatically added to the beginning of all relative URLs the browser loads (whether HREF or SRC attributes, etc).

Note: It will only work for relative HREF and SRC attributes... absolute ones (including those just beginning with /) will remain unchanged.

At least with <base> you only have to remember to change 1 line per file when uploading to your live site, rather than all URLs.

See for more.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Dan,

It was the <base> element that I was missing. I had a feeling I had seen something like that somewhere, but couldn't remember what it was. Thanks.

Re your suggestion for setting up a local web server. Yes, I guess I could do that. I already have Apache, which I've used for a small PHP project. I'll consider using it here.

But I'm now tending towards another solution. I think I'll organise the site so that the root only contains the home page - and perhaps odds and ends like 404 error pages - but no other content. I'll put all the real content in sub-directories, each of which will be immediately below the root.

That way, I can use relative addressing in all the paths. Every document I reference will either be in the same directory as the referrer, or in a sibling directory.

The only exception will be the home page, which I'll treat as a special case.

Does that sound like a reasonable approach? It's exactly what I do in other types of software development (such as Visual FoxPro projects).

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top