3.3 Related Topic: Naming Convention and Referencing相关主题:命名约定和引用
At this point, it’s necessary to go into the naming conventions for files, directories, and directory structures when referencing other content because you’ll make use of them repeatedly in the following sections and throughout this book. 此时,在引用其他内容时,有必要了解文件、目录和目录结构的命名约定,因为您将在以下部分和本书中反复使用它们。Mind you, this isn’t yet about the HTML elements or HTML tags you can use to link a web page, but only about how to write such a link to the target. 请注意,这还不是关于可以用来链接网页的HTML元素或HTML标签,而只是关于如何将这样的链接写入目标。If you’re already familiar with terms such as full URL or absolute or relative path, you can skip this section or just skim through it.如果您已经熟悉诸如完整URL或绝对或相对路径之类的术语,您可以跳过本节或只是浏览一下。
3.3.1 Valid and Good File Names for an HTML DocumentHTML文档的有效和良好的文件名
The use of file names for web pages has become quite flexible. Nevertheless, here are some guidelines and recommendations you can follow: Use only lowercase letters a–z, digits 0–9, hyphens, and underscores if possible. 网页文件名的使用变得非常灵活。然而,这里有一些你可以遵循的指导方针和建议:如果可能的话,只使用小写字母a–z、数字0–9、连字符和下划线。The dot is usually used only to separate the file extension. Whether you use uppercase letters in the file name is a matter of taste, but you should keep in mind that some systems are case sensitive while others aren’t.圆点通常仅用于分隔文件扩展名。文件名中是否使用大写字母是一个品味问题,但你应该记住,有些系统区分大小写,而另一些系统则不区分。
Good Names for the Search Engine搜索引擎的好名字
The file names can also be used to place keywords in them for the search engines. 文件名也可用于为搜索引擎在其中放置关键字。Instead of using the relatively meaningless domain.com/page01.html, you should choose a better name such as domain.com/smartphones.html. 与其使用相对无意义的domain.com/page01.html,你应该选择一个更好的名字,比如domain.com/smartphones.html。If there are several keywords in a file name, you should separate them with a hyphen. The underscore as a separator, on the other hand, is usually not evaluated by search engines as a word separator. 如果文件名中有多个关键字,则应使用连字符分隔它们。另一方面,下划线作为分隔符通常不会被搜索引擎评估为单词分隔符。Instead of domain.com/smartphonesandcellphones.html, where the keywords aren’t recognized, it’s better to use domain.com/smartphones-and-cellphones.html.与其使用无法识别关键字的domain.com/smartphonesandcellphones.html,不如使用domain.com/smartphones-and-cellphones.html。
3.3.2 Valid Directory Names and Meaningful Directory Structures有效的目录名称和有意义的目录结构
The same thing I just wrote for file names applies to the use of directory names. In addition to a meaningful directory name, a meaningful directory structure is also important. 我刚才为文件名所写的内容同样适用于目录名的使用。除了一个有意义的目录名,一个有含义的目录结构也很重要。Again, you may have the advantage that this structure with good directory names will have a positive effect on your page ranking with the search engines. For example, the following is a useful directory structure:同样,您可能有这样的优势,即这种具有良好目录名称的结构将对您在搜索引擎中的页面排名产生积极影响。例如,以下是一个有用的目录结构:
/smartphones
/smartphones/apple
/smartphones/android
/apps/apple
/apps/android
...
Thanks to a directory structure based on different topics and coupled with a good file name, you could, for example, call an HTML document named buy-ramsung-xyz.html as follows:得益于基于不同主题的目录结构和良好的文件名,例如,您可以按如下方式调用名为buy-ramsung-xyz.html的HTML文档:
domain.com/smartphones/android/buy-ramsung-xyz.html
On the other hand, you should refrain from using directory names that don’t carry much meaning for visitors or search engines, such as the following:另一方面,您应该避免使用对访问者或搜索引擎没有多大意义的目录名,例如以下内容:
/html
/html/pages
/contents/
/contents/pages
...
3.3.3 Writing a Reference to a Data Source编写对数据源的引用
Without the functionality to reference other content, the internet wouldn’t be what it is today. 如果没有引用其他内容的功能,互联网就不会是今天的样子。In addition to classic hyperlinks to other content, such referencing is also used for many other things, including images, external scripts, CSS files, or video resources. 除了指向其他内容的经典超链接外,这种引用还用于许多其他内容,包括图像、外部脚本、CSS文件或视频资源。For this reason, this section describes several ways to create a reference to other content.因此,本节介绍了创建对其他内容的引用的几种方法。
Simple Structure of Addresses on the Internet (URL)互联网地址的简单结构(URL)
There’s no comprehensive treatise at this point, but you should at least know the basic form of an address on the internet, also referred to as a Uniform Resource Locator (URL). 目前还没有全面的论述,但你至少应该知道互联网上地址的基本形式,也称为一致性资源定位符(URL)。Only thanks to this URL is it possible to use an address on the internet in a readable format and to access directories and documents. 只有通过这个URL,才能以可读的格式使用互联网上的地址,并访问目录和文档。A classic URL looks like this: http://www.domain.com/path/file.html.一个经典的URL看起来像这样:http://www.domain.com/path/file.html。
If you decompose this address, you’ll get the individual components listed in Table 3.2.如果你分解这个地址,你会得到表3.2中列出的各个组件。
|
|
|
|
---|---|---|---|
http:// |
www.domain.com |
path |
file.html |
Table 3.2
Rough Structure of an Internet Address互联网地址的粗略结构
You can use the protocol (or scheme) to specify how the resource should be used. 您可以使用协议(或方案)指定如何使用资源。http:// is the protocol for hypertext documents (HTTP). http://是超文本文档(http)的协议。Other well-known representatives are https:// for a secured data transfer, ftp:// for a file transfer (FTP), or file:// for the access to local files. 其他著名的代表是https://用于安全的数据传输,ftp://用于文件传输(ftp),或file://用于访问本地文件。With www.domain.com, you have a host name that’s converted to an IP address via the Domain Name System (DNS). 使用www.domain.com,您可以通过域名系统(DNS)将主机名转换为IP地址。It’s followed by the path specification, whose components are separated from each other by a /. 紧随其后的是路径规范,其组件之间用/
分隔。Finally, the file name (here, file.html) of the document you want to call is often specified as well.最后,通常还会指定要调用的文档的文件名(此处为file.html
)。
The www of www.domain.com is part of the host name and conveniently chosen to give an indication of the host’s intended use, for example, as a WWW or FTP server. www.domain.com
的www
是主机名的一部分,可以方便地选择它来指示主机的预期用途,例如作为www或FTP服务器。However, this isn’t a prerequisite. The name of a WWW server doesn’t necessarily have to start with www. 然而,这不是先决条件。WWW服务器的名称不一定必须以WWW开头。You can select the service running on the server with the protocol and port on which the service is listening, for example, port 80 for WWW or port 21 for FTP.您可以使用服务正在侦听的协议和端口选择服务器上运行的服务,例如,WWW的端口80或FTP的端口21。
In this example, with the host name www.domain.com, it’s a WWW domain name for a network server, where the ISO country code com is the top-level domain. 在这个例子中,主机名www.domain.com
是网络服务器的WWW域名,其中ISO国家代码com
是顶级域。The top-level domain is sorted either thematically (e.g., com, org, net) or geographically by country (e.g., de, at, ch). 顶级域名按主题(如com、org、net)或按国家(如de、at、ch)进行地理排序。domain is the second-level domain and the actual name of the server. 域是二级域和服务器的实际名称。Here, further subdomains (or sublevels) are possible, which lie below another one in the hierarchy. example.domain.com would thus be a subdomain of domain.com, for example.在这里,可以有更多的子域(或子级别),它们位于层次结构中的另一个子域之下。例如,example.domain.com
将是domain.com
的子域。
Homepage: "index.html"
If you enter an internet address such as http://www.domain.com/ in the address field of the web browser, you’ll still get a web page displayed in the browser even though you haven’t explicitly specified a path or file name there. 如果您输入互联网地址,例如http://www.domain.com/在web浏览器的地址字段中,即使您没有明确指定路径或文件名,您仍然会在浏览器中显示一个网页。This is because the web servers return a default page, depending on the setting. 这是因为web服务器根据设置返回默认页面。Many web servers return at least index.htm, index.html, or default.html if there’s a corresponding file in the root directory. 如果根目录中有相应的文件,许多web服务器至少会返回index.htm、index.html或default.html。This usually works with any other directory as well. 这通常也适用于任何其他目录。For example, if you enter http://www.domain.com/travel/ in the browser’s address bar and there’s an index.html file in the /travel directory, that file will be returned. 例如,如果您在浏览器的地址栏中输入http://www.domain.com/travel/,/travel目录中有一个index.html文件,将返回该文件。However, as already mentioned, it depends on the settings of the web server what can be returned or done and how this happens. 然而,如前所述,这取决于web服务器的设置,可以返回或执行什么以及如何发生。Some web hosts also allow you to define your own rules for this in .htaccess. 一些网络主机还允许您在.htaccess中为此定义自己的规则。The .htaccess file is a configuration file that can be used to make various settings and specifications about things such as access control, exclusion of addresses, error messages, password protection, and alternative content, among other things..htaccess
文件是一个配置文件,可用于对访问控制、地址排除、错误消息、密码保护和替代内容等进行各种设置和规范。
Using a Reference with Full URL to the Data Source使用指向数据源的完整URL的引用
When something is referenced with a full URL, we’re talking about the fully spelled out web address. 当用完整的URL引用某物时,我们谈论的是完整拼写的网址。You can use a full URL such as http://www.domain.com/travel/index.html or http://www.domain.com/pictures/foto.jpg if the data is located on a different machine (host name/domain) than the HTML document. 您可以使用完整的URL,例如http://www.domain.com/travel/index.html或http://www.domain.com/pictures/foto.jpg如果数据位于与HTML文档不同的机器(主机名/域)上。You already learned how a complete URL looks in detail in the previous section.在上一节中,您已经详细了解了完整URL的外观。
A Reference as an Absolute Path Specification Relative to the Base URL作为相对于基本URL的绝对路径规范的引用
If you reference something with an absolute path, the desired data is on the same computer as the HTML document. 如果使用绝对路径引用某个内容,则所需的数据与HTML文档位于同一台计算机上。If a web page is accessible via http://www.domain.com/travel/index.html, /travel/index.html represents the absolute path specification relative to the URL http://www.domain.com. 如果网页可通过以下方式访问http://www.domain.com/travel/index.html,/travel/index.html表示相对于URL的绝对路径规范http://www.domain.com。Thus, you can use this path specification for your web pages if the data is located within your domain (or subdomain).因此,如果数据位于您的域(或子域)内,您可以将此路径规范用于您的网页。
Root Directory根目录
The root directory / (the highest directory to reach) of a domain such as http://www.domain.com is often set as the document start directory by the web server when the domain is configured. 域的根目录/(可访问的最高目录),例如http://www.domain.com在配置域时,通常由web服务器设置为文档开始目录。For example, if you connect to an FTP client and want to upload your web pages, this root directory can also be inside a directory named www, htdocs, web, and so on. 例如,如果你连接到FTP客户端并想上传你的网页,这个根目录也可以在一个名为www、htdocs、web等的目录中。Nevertheless, the root directory for http://www.domain.com is usually still / and not /www, /htdocs, or /web. 然而,根目录http://www.domain.com通常是/而不是/www、/htdocs或/web。However, this again depends on the configuration implemented by your web hosting provider, whom you should contact in case of doubt.但是,这再次取决于您的网络托管提供商所实施的配置,如有疑问,您应该联系他们。
Specifying a Reference with a Relative Path to the Data Source指定具有数据源相对路径的引用
You can use a relative path specification if you use the current address as the reference address. 如果使用当前地址作为引用地址,则可以使用相对路径规范。For example, if you’re at the full URL http://www.domain.com/travel/index.html, and there’s an image named photo.jpg in the /travel directory, you can reference this file with a relative path specification such as photo.jpg or ./photo.jpg. 例如,如果您位于完整的URLhttp://www.domain.com/travel/index.html,/travel目录中有一个名为photo.jpg的图像,您可以使用相对路径规范(如photo.jpg或)引用此文件/照片jpg。Alternatively, you could use the absolute path specification with /travel/photo.jpg or the absolute URL with http://www.domain.com/travel/photo.jpg. The use of the absolute URL is uncommon in such cases.或者,您可以将绝对路径规范与/travel/photo.jpg一起使用,或与绝对URLhttp://www.domain.com/travel/photo.jpg一起使用。在这种情况下,使用绝对URL是不常见的。
If photo.jpg or ./photo.jpg is specified as a relative path, it’s assumed that the file is located in the same directory. 如果photo.jpg或./photo.jpg被指定为相对路径,假设文件位于同一目录中。If you want to reference a file in a directory one level above, you can use ... 如果你想引用上面一级目录中的文件,你可以使用..。For example, if you want to access photo.jpg in the /travel directory from the full URL http://www.domain.com/travel/california/index.html as a relative path, you could do so by using ../photo.jpg. 例如,如果您想从完整URLhttp://www.domain.com/travel/california/index.html以相对路径的方式访问/travel目录中的photo.jpg,您可以使用../photo.jpg。This way, you reference the directory above the current directory.这样,您可以引用当前目录上方的目录。