Html 链接不回目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4862231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:27:41  来源:igfitidea点击:

Links not going back a directory?

htmlbroken-links

提问by zik

this is probably a silly question and I have the answer myself but I want to know if I am doing anything wrong. I have a website, let's call it www.mysite.com. Within this site, I have some FAQs but the person that built the site saved the FAQ pages under a directory on the site named "FAQs".

这可能是一个愚蠢的问题,我自己有答案,但我想知道我是否做错了什么。我有一个网站,我们称之为www.mysite.com。在这个站点中,我有一些常见问题解答,但是建立站点的人将常见问题解答页面保存在站点上名为“常见问题解答”的目录下。

As an example an FAQ page would be located at:

例如,常见问题页面位于:

www.mysite.com/pages/en/faqs/faq-page1.html.

www.mysite.com/pages/en/faqs/faq-page1.html.

Note the pages/en/directory. Ideally I would like all the pages to be saved under www.mysite.com/index.htmletc but I can't change this.

注意pages/en/目录。理想情况下,我希望所有页面都保存在www.mysite.com/index.htmletc下,但我无法更改。

Anyway, when I am on any of these FAQ pages, and I try to link back to say the home page index.htmlthe navigation won't go to the page. So for example, when I am on:

无论如何,当我在这些常见问题解答页面中的任何一个页面上时,我尝试链接回主页index.html,导航不会转到该页面。例如,当我在:

www.mysite.com/pages/en/faqs/faq-page1.html

www.mysite.com/pages/en/faqs/faq-page1.html

and I try to link back to the home page

我尝试链接回主页

www.mysite.com/pages/en/index.html(which is where the index page is saved) the nav won't work. Instead it will try to go to www.mysite.com/pages/en/faqs/index.html.

www.mysite.com/pages/en/index.html(这是保存索引页面的位置)导航将不起作用。相反,它会尝试转到www.mysite.com/pages/en/faqs/index.html.

Now I am assuming this happens because I am in the "faq" directory, but how do I go back to the root directory when linking? The code for the link is simply <a href="index.html">Home</a>. I could of course just put in the full link www.mysite.com/pages/en/index.html, which would solve this but is there another way around this? Sorry for such a long post and I may have been able to explain this better but I can't :S

现在我假设发生这种情况是因为我在“faq”目录中,但是在链接时如何返回根目录?链接的代码很简单<a href="index.html">Home</a>。我当然可以放入完整链接www.mysite.com/pages/en/index.html,这可以解决这个问题,但还有其他方法吗?很抱歉发表这么长的帖子,我可能能够更好地解释这一点,但我不能:S

Thanks in advance.

提前致谢。

回答by Robb

You need to give a relative file path of <a href="../index.html">Home</a>

您需要提供一个相对文件路径 <a href="../index.html">Home</a>

Alternately you can specify a link from the root of your site with <a href="/pages/en/index.html">Home</a>

或者,您可以从站点的根目录指定一个链接 <a href="/pages/en/index.html">Home</a>

..and .have special meanings in file paths, ..means up one directory and .means current directory.

...在文件路径中具有特殊含义,..表示向上一个目录,.表示当前目录。

so <a href="index.html">Home</a>is the same as <a href="./index.html">Home</a>

所以<a href="index.html">Home</a>是一样的<a href="./index.html">Home</a>

回答by álvaro González

There are two type of paths: absolute and relative. This is basically the same for files in your hard disc and directories in a URL.

有两种类型的路径:绝对路径和相对路径。这对于硬盘中的文件和 URL 中的目录来说基本相同。

Absolute paths start with a leading slash. They always point to the same location, no matter where you use them:

绝对路径以斜杠开头。无论您在哪里使用它们,它们始终指向相同的位置:

  • /pages/en/faqs/faq-page1.html
  • /pages/en/faqs/faq-page1.html

Relative paths are the rest (all that do not start with slash). The location they point to dependson where you are using them

其余的是相对路径(所有不以斜杠开头的)。它们指向的位置取决于您使用它们的位置

  • index.htmlis:
    • /pages/en/faqs/index.htmlif called from /pages/en/faqs/faq-page1.html
    • /pages/index.htmlif called from /pages/example.html
    • etc.
  • index.html是:
    • /pages/en/faqs/index.html如果从 /pages/en/faqs/faq-page1.html
    • /pages/index.html如果从 /pages/example.html
    • 等等。

There are also two special directory names: .and ..:

还有两个特殊的目录名称:...

  • .means "current directory"
  • ..means "parent directory"
  • .意思是“当前目录”
  • ..意思是“父目录”

You can use them to build relative paths:

您可以使用它们来构建相对路径:

  • ../index.htmlis /pages/en/index.htmlif called from /pages/en/faqs/faq-page1.html
  • ../../index.htmlis /pages/index.htmlif called from /pages/en/faqs/faq-page1.html
  • ../index.html/pages/en/index.html如果从/pages/en/faqs/faq-page1.html
  • ../../index.html/pages/index.html如果从/pages/en/faqs/faq-page1.html

Once you're familiar with the terms, it's easy to understand what it's failing and how to fix it. You have two options:

一旦您熟悉了这些术语,就很容易理解它的问题是什么以及如何解决它。您有两个选择:

  • Use absolute paths
  • Fix your relative paths
  • 使用绝对路径
  • 修复您的相对路径

回答by lonesomeday

To go up a directory in a link, use ... This means "go up one directory", so your link will look something like this:

要在链接中上移目录,请使用... 这意味着“向上一个目录”,因此您的链接将如下所示:

<a href="../index.html">Home</a>