我应该对网站内的页面使用相对路径吗? 或者没关系?

发布于 2024-07-25 18:59:02 字数 559 浏览 5 评论 0原文

我只是想知道定义链接到域名内页面的 url 的最佳方法是什么。 例如,对于域名 www.example.com .. 我可以有类似

test 的 链接>

test

我遇到的问题之一是在使用模板时..如果我使用带有相对链接的 html 模板那么我不能对目录 www.example.com/directory2 使用相同的模板,因为公共链接不适用于该页面.. css 和 javascripts 文件都不起作用。

我要么必须复制模板,要么绝对设置路径。 我选择的是绝对路径。 因此模板路径以 http://www.mydomain.com/ 开头。这是否会导致任何性能问题例如多个 http 请求、域名解析或其他任何内容? 您对此有何建议/评论?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

生来就爱笑 2024-08-01 18:59:02

一定要使用相对路径。

这将为您提供同样多的功能,并且在您的域名更改时不会出现问题,并且允许您在不同的服务器上测试,而不是在现场……

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

枉心 2024-08-01 18:59:02

您可以根所有路径,

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

这与放置域相同。

[编辑] 您可以使用 mod rewrite 然后将站点移动到不同的文件夹(如果需要) - 请参阅
http://httpd.apache.org/docs/2.0/mod/ mod_rewrite.html#rewritebase

乔什

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

爱,才寂寞 2024-08-01 18:59:02

虽然使用绝对路径不会增加任何额外的开销,但本地链接的公认做法是使用相对链接。 wget 或 Down-Them-All(网站下载工具)等工具可以将相对链接与绝对链接区别对待。

但是,如果您的模板解决方案在使用绝对链接时效果最佳,那么坚持使用它们也并非闻所未闻。 暂时,如果您的网站内容通过链接回原始页面而被抓取,绝对链接可以对您有利(来源:http://www.navegabem.com/linking-strategy.html)。

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

旧城空念 2024-08-01 18:59:02

我想你应该使用相对路径。 因为如果您的域名消失/更改,该网站仍然可以正常工作,而且更重要的是,html 中的路径会短得多。

如果你想适应改变文件结构的情况。
假设您正在使用 html 来开发网站。
我想你可以利用 shtml / 服务器端包含来分隔常见项目,例如:
菜单

这样当结构发生变化时,您只需修改 menu.shtml

希望这有帮助

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

葬﹪忆之殇 2024-08-01 18:59:02

乔希的建议是可以的,但如果你的网站放在虚拟目录下,就会失败。 我建议始终使用相对路径。

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

落在眉间の轻吻 2024-08-01 18:59:02

始终使用相对路径来引用站点中的资源(页面、CSS 文件、js 文件)。

原因:

  1. 这使您可以随时更改域名,而不会带来巨大的麻烦。
  2. 让您可以轻松地重组页面。

但是,如果您的网站真的那么大,您将需要有一个链接和 linkid 数据库,以免在移动资源时放错链接。 这就是微软在 microsoft.com 上所做的事情。

杰瑞赫

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

伏妖词 2024-08-01 18:59:02

我和 Josh 一起......指定相对于应用程序根目录的路径:/myfolder/myfile.html

在我的情况下,这具有在我的开发计算机上工作的额外优势,其中每个站点都是我的应用程序(IIS)默认站点,以及在实时站点本身上工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文