问题
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.
回答1:
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).
回答2:
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....
回答3:
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:
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:
What Josh sugested is ok, but it will fail if your website is placed under a virtual dir. I recomend always using relative paths.
回答6:
Always your relative paths to refer to resources (pages, css files, js files) that are in your site.
Reasons:
- This allows you to change your domain name any time without incurring humoungous amounts of headache
- 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
回答7:
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.
来源:https://stackoverflow.com/questions/1016917/should-i-use-relative-path-for-pages-within-the-site-or-it-doesnt-matter