I've uploaded several files to my server and it's really quite baffling. The home page is saved as index.html
, and when I type in the URL of said page it miraculously, and quite successfully shows the right page. What about my other pages? I have linked to them from the home page with the following code:
我已将几个文件上传到我的服务器,这真是令人费解。主页保存为index.html,当我输入所述页面的URL时,它奇迹般地,并且非常成功地显示了正确的页面。我的其他页面怎么样?我已从主页链接到它们,代码如下:
<a href="http://www.example.com/about/">About Us</a>
How does my html file, presumably called about.html, supposed to know that its URL is "http://www.example.com/about/"? I am dubbing this "The Unanswered Question" because I have looked at numerous examples of metadata and there is nothing about specifying the URL of a page.
我的html文件,大概叫做about.html,应该知道它的URL是“http://www.example.com/about/”?我正在配音这个“未答复的问题”,因为我查看了大量的元数据示例,而且没有任何关于指定页面的URL的信息。
6 个解决方案
#1
3
It depends on what type of server you are running.
这取决于您运行的服务器类型。
Static web servers
If it is the simplest kind of static file server with no URL aliasing or rewriting then URLs will map directly to files:
如果它是最简单的静态文件服务器,没有URL别名或重写,那么URL将直接映射到文件:
If your "web root" was /home/youruser/www/
, then that means:
如果您的“web root”是/ home / youruser / www /,则表示:
http://www.example.com -> /home/youruser/www/
And any paths (everything after the domain name) translate directly to paths under that web root:
任何路径(域名后面的所有内容)都直接转换为该Web根目录下的路径:
http://www.example.com/about.html -> /home/youruser/www/about.html
Usually web servers will look automatically for an "index.html" file if no file is specified (i.e. the URL ends in a /
):
如果没有指定文件(即URL以/结尾),通常Web服务器将自动查找“index.html”文件:
http://www.example.com/ -> /home/youruser/www/index.html
http://www.example.com/about/ -> /home/youruser/www/about/index.html
In Apache, the filename searched for is configurable with the DirectoryIndex
directive:
在Apache中,搜索的文件名可以使用DirectoryIndex指令进行配置:
DirectoryIndex index.html index.txt /cgi-bin/index.pl
That means that every request to a path that ends in a /
(and to add yet another rule, under some common settings it will automatically append a /
if the path is the name of a directory, for example 'about'):
这意味着对以/结尾的路径的每个请求(并且在一些常见设置下添加另一个规则,它将自动附加/如果路径是目录的名称,例如'about'):
http://www.example.com/ -> /home/youruser/www/index.html
-> or /home/youruser/www/index.txt
-> or /home/youruser/www/cgi-bin/index.pl
Web servers with path interpretation
There are too many different types of servers which perform this functionality to list them all, but the basic idea is that a request to the server is captured by a program and then the program decides what to output based on the path.
有太多不同类型的服务器执行此功能以将它们全部列出,但基本思想是程序捕获对服务器的请求,然后程序根据路径决定输出什么。
For example, a program might perform different routes for basic matching rules:
例如,程序可能会为基本匹配规则执行不同的路由:
*.(gif|jpg|css|js) -> look for and return the file from /home/user/static
blog/* -> send to a "blog" program to generate the resulting page
using a combination of templates and database resources
Examples include:
- Python
- Java Servlets
- Apache mod_rewrites (used by Wordpress, etc.)
Apache mod_rewrites(由Wordpress等使用)
Links in HTML pages
Finally, the links in the HTML pages just change the URL of the location bar. The behavior of an HTML link is the same regardless of what exists on the server. And the server, in turn, only responds to HTTP requests and only produces resources (HTML, images, CSS, JavaScript, etc.), which your browser consumes. The server only serves those resources and does not have any special behavioral link with them.
最后,HTML页面中的链接只会更改位置栏的URL。无论服务器上存在什么,HTML链接的行为都是相同的。反过来,服务器只响应HTTP请求,只生成浏览器消耗的资源(HTML,图像,CSS,JavaScript等)。服务器仅为这些资源提供服务,并且没有任何特殊的行为链接。
-
Absolute URLs are those that start with a scheme (such as
http:
as you have done). The whole content of the location bar will be replaced with this when the user clicks the link. -
Domain relative URLs are those that start with a forward slash (
/
). Everything after the domain name will be replaced with the contents of this link. -
Relative URLs are everything else. Everything after the last directory (
/
) in the URL will be replaced with the contents of this link.
绝对URL是以方案开头的URL(例如http:就像你所做的那样)。当用户单击链接时,位置栏的全部内容将替换为此内容。
域相对URL是以正斜杠(/)开头的URL。域名后面的所有内容都将替换为此链接的内容。
相对URL是其他一切。 URL中最后一个目录(/)之后的所有内容都将替换为此链接的内容。
Examples:
- My page on "mydomain.com" can link to your site using the
<a href="http://www.example.com/about/">Example.com about</a>
just as you have done. - If I change my links to
<a href="/about">about</a>
then it will link tomydomain.com
instead.
我的“mydomain.com”页面可以使用 Example.com关于链接到您的网站,就像您一样。
如果我将链接更改为约,则会链接到mydomain.com。
An answer your question
How does my html file, presumably called about.html, supposed to know that its URL is "http://www.example.com/about/"?
我的html文件,大概叫做about.html,应该知道它的URL是“http://www.example.com/about/”?
First, the file itself has no idea what its URL is. Unless:
首先,文件本身不知道它的URL是什么。除非:
- the HTML was dynamically generated using a program. Most server-side languages provide a way to get this.
- after the page is served, client-side scripts can also detect the current URL
HTML是使用程序动态生成的。大多数服务器端语言提供了一种获取此方法的方法。
在提供页面之后,客户端脚本也可以检测当前URL
Second, if the URL is /about
and the file is actually about.html
then you probably have some kind of rewriting going on. Remember that paths, in their simplest, are literal translations and /about
is not the same as about.html
.
其次,如果URL是/ about并且文件实际上是about.html那么你可能正在进行某种重写。请记住,最简单的路径是字面翻译和/ about与about.html不同。
#2
1
Just use /about.html
to link to the page
只需使用/about.html链接到该页面即可
#3
1
Theoretically, it's better for URLs in your documents to be relative, so that you don't have to change them in the event you change the domain or the files location.
从理论上讲,文档中的URL最好是相对的,这样您就不必在更改域或文件位置时更改它们。
For example, if you move it from localhost to your hosted server.
例如,如果将其从localhost移动到托管服务器。
In your example, instead of www.example.com/about.html
use /about.html
.
在您的示例中,使用/about.html而不是www.example.com/about.html。
#4
0
Given the link above you would need a about page named index.html located in a directory named about for your example to work. That is however not common practice.
鉴于上面的链接,您需要一个名为index.html的页面,该页面位于名为about的目录中,以便您的示例正常工作。然而,这不常见。
#5
0
I'm a bit confused, but here is some information. Any file named "index" is the default display page for any directory(folder) when trying to view that directory.
我有点困惑,但这里有一些信息。任何名为“index”的文件都是尝试查看该目录时任何目录(文件夹)的默认显示页面。
All files in a folder are always relative to that directory. So if your link is in a file, within a different directory, then you must type in that directory along with the file. If it is the same directory, then there is no need to type in that directory, just the file name.
文件夹中的所有文件始终相对于该目录。因此,如果您的链接位于不同目录中的文件中,则必须在该目录中键入该文件。如果它是同一目录,则无需键入该目录,只需输入文件名。
#6
0
about.html
doesn't know what it's URL is, its the index.html
file that calls your about.html
file.
about.html不知道它的URL是什么,它是调用你的about.html文件的index.html文件。
When you're in any given directory, linking to other pages within that directory is done via a simple relative link:
当您在任何给定目录中时,通过简单的相对链接链接到该目录中的其他页面:
<a href="/about.html">About Us</a>
Moving up a directory, assuming you're in a sub folder (users) perhaps you can use the ..
operator to navigate up one directory:
提升目录,假设您在子文件夹(用户)中,也许您可以使用..运算符导航一个目录:
<a href="../about.html">About Us</a>
In your case your about page is in the same directory as the page you're linking from so it just goes to the right page.
在您的情况下,您的about页面与您要链接的页面位于同一目录中,因此它只是转到右侧页面。
Additionally (and I think this may be what you're asking) if you have:
另外(如果你有以下情况,我认为这可能是你要问的):
about.html
about.php
about.phtml
about.jpgabout.html about.php about.phtml about.jpg
for example, and you visit http://www.yoursite.com/about
it will automatically bring up the html page and the other pages should be referenced explicitly somewhere if you want them to be used.
例如,你访问http://www.yoursite.com/about它会自动显示html页面,如果你想要使用它们,其他页面应该在某处明确引用。
#1
3
It depends on what type of server you are running.
这取决于您运行的服务器类型。
Static web servers
If it is the simplest kind of static file server with no URL aliasing or rewriting then URLs will map directly to files:
如果它是最简单的静态文件服务器,没有URL别名或重写,那么URL将直接映射到文件:
If your "web root" was /home/youruser/www/
, then that means:
如果您的“web root”是/ home / youruser / www /,则表示:
http://www.example.com -> /home/youruser/www/
And any paths (everything after the domain name) translate directly to paths under that web root:
任何路径(域名后面的所有内容)都直接转换为该Web根目录下的路径:
http://www.example.com/about.html -> /home/youruser/www/about.html
Usually web servers will look automatically for an "index.html" file if no file is specified (i.e. the URL ends in a /
):
如果没有指定文件(即URL以/结尾),通常Web服务器将自动查找“index.html”文件:
http://www.example.com/ -> /home/youruser/www/index.html
http://www.example.com/about/ -> /home/youruser/www/about/index.html
In Apache, the filename searched for is configurable with the DirectoryIndex
directive:
在Apache中,搜索的文件名可以使用DirectoryIndex指令进行配置:
DirectoryIndex index.html index.txt /cgi-bin/index.pl
That means that every request to a path that ends in a /
(and to add yet another rule, under some common settings it will automatically append a /
if the path is the name of a directory, for example 'about'):
这意味着对以/结尾的路径的每个请求(并且在一些常见设置下添加另一个规则,它将自动附加/如果路径是目录的名称,例如'about'):
http://www.example.com/ -> /home/youruser/www/index.html
-> or /home/youruser/www/index.txt
-> or /home/youruser/www/cgi-bin/index.pl
Web servers with path interpretation
There are too many different types of servers which perform this functionality to list them all, but the basic idea is that a request to the server is captured by a program and then the program decides what to output based on the path.
有太多不同类型的服务器执行此功能以将它们全部列出,但基本思想是程序捕获对服务器的请求,然后程序根据路径决定输出什么。
For example, a program might perform different routes for basic matching rules:
例如,程序可能会为基本匹配规则执行不同的路由:
*.(gif|jpg|css|js) -> look for and return the file from /home/user/static
blog/* -> send to a "blog" program to generate the resulting page
using a combination of templates and database resources
Examples include:
- Python
- Java Servlets
- Apache mod_rewrites (used by Wordpress, etc.)
Apache mod_rewrites(由Wordpress等使用)
Links in HTML pages
Finally, the links in the HTML pages just change the URL of the location bar. The behavior of an HTML link is the same regardless of what exists on the server. And the server, in turn, only responds to HTTP requests and only produces resources (HTML, images, CSS, JavaScript, etc.), which your browser consumes. The server only serves those resources and does not have any special behavioral link with them.
最后,HTML页面中的链接只会更改位置栏的URL。无论服务器上存在什么,HTML链接的行为都是相同的。反过来,服务器只响应HTTP请求,只生成浏览器消耗的资源(HTML,图像,CSS,JavaScript等)。服务器仅为这些资源提供服务,并且没有任何特殊的行为链接。
-
Absolute URLs are those that start with a scheme (such as
http:
as you have done). The whole content of the location bar will be replaced with this when the user clicks the link. -
Domain relative URLs are those that start with a forward slash (
/
). Everything after the domain name will be replaced with the contents of this link. -
Relative URLs are everything else. Everything after the last directory (
/
) in the URL will be replaced with the contents of this link.
绝对URL是以方案开头的URL(例如http:就像你所做的那样)。当用户单击链接时,位置栏的全部内容将替换为此内容。
域相对URL是以正斜杠(/)开头的URL。域名后面的所有内容都将替换为此链接的内容。
相对URL是其他一切。 URL中最后一个目录(/)之后的所有内容都将替换为此链接的内容。
Examples:
- My page on "mydomain.com" can link to your site using the
<a href="http://www.example.com/about/">Example.com about</a>
just as you have done. - If I change my links to
<a href="/about">about</a>
then it will link tomydomain.com
instead.
我的“mydomain.com”页面可以使用 Example.com关于链接到您的网站,就像您一样。
如果我将链接更改为约,则会链接到mydomain.com。
An answer your question
How does my html file, presumably called about.html, supposed to know that its URL is "http://www.example.com/about/"?
我的html文件,大概叫做about.html,应该知道它的URL是“http://www.example.com/about/”?
First, the file itself has no idea what its URL is. Unless:
首先,文件本身不知道它的URL是什么。除非:
- the HTML was dynamically generated using a program. Most server-side languages provide a way to get this.
- after the page is served, client-side scripts can also detect the current URL
HTML是使用程序动态生成的。大多数服务器端语言提供了一种获取此方法的方法。
在提供页面之后,客户端脚本也可以检测当前URL
Second, if the URL is /about
and the file is actually about.html
then you probably have some kind of rewriting going on. Remember that paths, in their simplest, are literal translations and /about
is not the same as about.html
.
其次,如果URL是/ about并且文件实际上是about.html那么你可能正在进行某种重写。请记住,最简单的路径是字面翻译和/ about与about.html不同。
#2
1
Just use /about.html
to link to the page
只需使用/about.html链接到该页面即可
#3
1
Theoretically, it's better for URLs in your documents to be relative, so that you don't have to change them in the event you change the domain or the files location.
从理论上讲,文档中的URL最好是相对的,这样您就不必在更改域或文件位置时更改它们。
For example, if you move it from localhost to your hosted server.
例如,如果将其从localhost移动到托管服务器。
In your example, instead of www.example.com/about.html
use /about.html
.
在您的示例中,使用/about.html而不是www.example.com/about.html。
#4
0
Given the link above you would need a about page named index.html located in a directory named about for your example to work. That is however not common practice.
鉴于上面的链接,您需要一个名为index.html的页面,该页面位于名为about的目录中,以便您的示例正常工作。然而,这不常见。
#5
0
I'm a bit confused, but here is some information. Any file named "index" is the default display page for any directory(folder) when trying to view that directory.
我有点困惑,但这里有一些信息。任何名为“index”的文件都是尝试查看该目录时任何目录(文件夹)的默认显示页面。
All files in a folder are always relative to that directory. So if your link is in a file, within a different directory, then you must type in that directory along with the file. If it is the same directory, then there is no need to type in that directory, just the file name.
文件夹中的所有文件始终相对于该目录。因此,如果您的链接位于不同目录中的文件中,则必须在该目录中键入该文件。如果它是同一目录,则无需键入该目录,只需输入文件名。
#6
0
about.html
doesn't know what it's URL is, its the index.html
file that calls your about.html
file.
about.html不知道它的URL是什么,它是调用你的about.html文件的index.html文件。
When you're in any given directory, linking to other pages within that directory is done via a simple relative link:
当您在任何给定目录中时,通过简单的相对链接链接到该目录中的其他页面:
<a href="/about.html">About Us</a>
Moving up a directory, assuming you're in a sub folder (users) perhaps you can use the ..
operator to navigate up one directory:
提升目录,假设您在子文件夹(用户)中,也许您可以使用..运算符导航一个目录:
<a href="../about.html">About Us</a>
In your case your about page is in the same directory as the page you're linking from so it just goes to the right page.
在您的情况下,您的about页面与您要链接的页面位于同一目录中,因此它只是转到右侧页面。
Additionally (and I think this may be what you're asking) if you have:
另外(如果你有以下情况,我认为这可能是你要问的):
about.html
about.php
about.phtml
about.jpgabout.html about.php about.phtml about.jpg
for example, and you visit http://www.yoursite.com/about
it will automatically bring up the html page and the other pages should be referenced explicitly somewhere if you want them to be used.
例如,你访问http://www.yoursite.com/about它会自动显示html页面,如果你想要使用它们,其他页面应该在某处明确引用。