Should I use relative path for pages within the si

2019-08-13 04:14发布

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">test</a>

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.

7条回答
smile是对你的礼貌
2楼-- · 2019-08-13 04:28

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).

查看更多
相关推荐>>
3楼-- · 2019-08-13 04:28

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

查看更多
萌系小妹纸
4楼-- · 2019-08-13 04:35

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

查看更多
混吃等死
5楼-- · 2019-08-13 04:35

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

查看更多
劫难
6楼-- · 2019-08-13 04:35

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.

查看更多
虎瘦雄心在
7楼-- · 2019-08-13 04:51

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

查看更多
登录 后发表回答