views:

282

answers:

7

Hi

I was just wondering what is the best way to define urls linking to pages within a domain name. For example, for domain www.example.com .. I can have links like

<a href="http://www.domain.com/test.html"&gt;test&lt;/a&gt;

or

<a href="test.html">test</a>

One of the issues, which I came across was that while using templates .. if I use html template with relative links then I can't use the same template for directory www.example.com/directory2 as the common links won't work for that page .. neither the css nor javascripts files.

I either had to duplicate the template or set paths absolutely. I chose absolute paths. so template paths start with http://www.mydomain.com/ .. Does this cause any performance issue like multiple http requests, domain resolving, or any other? what is your suggesstion/comment on this?

Thanks.

+1  A: 

you can root all your paths

eg <a href="/test.html">test</a>

this will be the same as putting the domain.

[EDIT] you can use mod rewrite to then move the site if needed to different folders - see http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritebase

Josh

Josh
-1: This will stop you from moving your website around: imagine having to always install your blog in the root of your website (at example.com) instead of doing so in a subfolder (at example.com/blog). Frustration!
Here Be Wolves
@harshath: in IIS, your blog folder would be an application. Not sure if there is a parallel in apache.
CJM
it would still be better than putting the full domain in and you could use mod_rewrite to change the root folder.
Josh
+1  A: 

I guess you should use relative path. Since if your domain's gone/changed, the site would still working properly, and more than that, the path in html would be much shorter.

And if you want to accommodate the case of changing file structure. Assuming you are using html for developing website. I guess you could make use of shtml / server side include, to separate common items, like: Menu

Such that when there's a change in structures, you simply have to modify the menu.shtml

Hope this helps

Zteeth
A: 

What Josh sugested is ok, but it will fail if your website is placed under a virtual dir. I recomend always using relative paths.

Sergio
+3  A: 

Definitely use relative paths.

This will give you no less functionality, and will stop you having issues if your domain name changes, and will allow you to test on a different server, rather than in live....

ck
This also allows for your site to be accessible from multiple different url's for example www.mydomain.com and www.mydomain.co.uk
Kragen
@kragen2uk rooting without the domain will also allow this. the problem with relative paths is when you are using templates that are then used in multiple folders - how do you reference the images/links so they are correct from any path?
Josh
@Josh - whilst routing solves many other problems, it does also mean you could never run your site in a folder, it would always have to relate to the root.
ck
@ck - not so sure about that - you could use mod_rewrite to put it in a folder. maybe the best idea is to root it in a folder then rewrite as necessary ie /mysite/test.html then if you want to have it in the root you'd just rewrite / to /mysite but if you want it elsewhere you just change the rewrite to /foo to /mysite - what do you think?
Josh
@Josh - thats possible, but you may want to put the html files etc. in a folder, and also present it in a folder. e.g. http://www.tempuri.org/ move to http://www.tempuri.org/oldsite
ck
+1  A: 

Always your relative paths to refer to resources (pages, css files, js files) that are in your site.

Reasons:

  1. This allows you to change your domain name any time without incurring humoungous amounts of headache
  2. Lets you restructure your pages very easily.

However, if you website is really that large, you will need to have a database of links and linkids so as not to misplace links while moving resources around. This is what microsoft does on microsoft.com.

jrh

Here Be Wolves
A: 

I'm with Josh.... specify paths relative to the application root: /myfolder/myfile.html

In my case, this has the added advantage of working on my development machine, where each site is an application (IIS) off my default site, as well as working on the live site itself.

CJM
+1  A: 

While using the absolute path does not add any additional overhead, the accepted practice for local links is to use relative links. Relative links can be treated differently from absolute links by such tools as wget or Down-Them-All (website downloading tools).

However, if your templating solution works best with absolute links, then it is not unheard of to stick with them. Tentatively, absolute links can work to your advantage if your site's content is scraped by linking back to your original page (Source: http://www.navegabem.com/linking-strategy.html).

Jon