HTML 和 CSS 的基本 URL

发布于 2024-10-19 16:19:12 字数 517 浏览 3 评论 0原文

我有一个问题,虽然我可以找到相关信息,但我想这是否正是我想知道的内容。

问题是,我在 http://localhost/site 上有一个网站。
现在,当我创建链接时,例如 ,它链接到 http://localhost/posts 而不是 <代码>http://localhost/site/posts。
如果我删除斜杠(
),效果很好,这将是最接近的,也许是最简单的解决方案,但我想知道为什么链接会忽略文件所在的文件夹?

我还想知道是否可以通过 .htaccess 或其他东西来解决这个问题。

我读过以 / 开头的链接使其成为“绝对”。因此,以 / 开头的链接仅用于直接链接到根目录,或者由存储在根目录中的站点使用(在这种情况下,它没有多大意义?)?

I got a question and although I could find related information, I'm if it was exactly about what I'm wondering about.

The thing is, I got a site on http://localhost/site.
Now, when I create a link, let's say, <a href="/posts">, it links to http://localhost/posts instead of http://localhost/site/posts.
It works fine if I remove the slash (<a href="posts">), that would be the closest and maybe the easiest solution, but I'd like to know why the links ignore the folder where the file is at?

And I also would like to know if this can be fixed with .htaccess or something.

I've read that a link that begins with / makes it "absolute". So a link beginning with / is only intended to be used to link directly to the root, or to be used by sites stored at the root (in this case it wouldn't make much sense?) ?

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

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

发布评论

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

评论(3

意犹 2024-10-26 16:19:12

URL 开头的前导“/”通知 Web 浏览器给定的路径是绝对(相对于 Web 服务器根),即如果您链接到 /posts 那么您就知道生成的链接将指向 http://www.mysite.com/posts

如果您不提供前导“/”(并且您不提供完整的 url,如 http://www.mysite.com/posts),则通常该 url 是相对的,并且给出的任何页面都将与当前正在查看的页面相关。

例如:

page being viewed               link url             target page
------------------------------------------------------------------------------
www.mysite.com/site              link.html            www.mysite.com/site/link.html
www.mysite.com/site              ../link.html         www.mysite.com/link.html
www.mysite.com/some/other/page   link.html            www.mysite.com/some/other/page/link.html
www.mysite.com/some/other/page   ../../../link.html   www.mysite.com/link.html

是否使用绝对链接或相对链接的决定完全取决于您 - 相对链接的优点是,如果您的网站移动,您网站上页面之间的链接仍然可以正常工作(例如,如果您的网站移动到 < code>www.mysite.com/otherpath,那么任何绝对链接(例如 www.mysite.com/originalpath/home)将不再起作用,

您应该看到以下站点以获得更完整的信息 。相对网址说明:

The leading '/' at the start of the URL informs the web browser that the path given is absolute (with respect to the web server root), i.e. if you link to /posts then you know that the resulting link will be to http://www.mysite.com/posts.

If you don't supply the leading '/' (and you don't give a complete url like http://www.mysite.com/posts) then usually the url is relative, and any page given will be relatvie to the page currently being viewed.

For example:

page being viewed               link url             target page
------------------------------------------------------------------------------
www.mysite.com/site              link.html            www.mysite.com/site/link.html
www.mysite.com/site              ../link.html         www.mysite.com/link.html
www.mysite.com/some/other/page   link.html            www.mysite.com/some/other/page/link.html
www.mysite.com/some/other/page   ../../../link.html   www.mysite.com/link.html

The decision on whether to use absolute or relative links is entirely up to you - the advantage of relative links is that if your site moves, links between pages on your site will still work correctly (for example if your site moves to www.mysite.com/otherpath, then any absolute links such www.mysite.com/originalpath/home will no longer work.

You should see the following site for a more complete explanation of relative urls:

往事风中埋 2024-10-26 16:19:12

您的站点根目录是 localhost,尽管您假设 site 是您的站点根目录。当您使用 / 时,它是相对于 localhost 的,因为它是绝对链接。

Your site root is localhost although you assume that site is your site root. When you use / it is relative to localhost as it is an absolute link.

你的往事 2024-10-26 16:19:12

尝试做一下< a href="../posts" >

./ 表示基目录或主目录

../ 表示上一级目录

Try doing it < a href="../posts" >

./ Means base directory, or home

../ Means one directory up

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