为什么有些网站的网址不包含文件扩展名?
我在浏览互联网时注意到,例如,YouTube 包含这样的 URL 来表示视频页面:http://www.youtube.com/watch?v=gwS1tGLB0vc
。
我的网站使用如下 URL 作为主题页面:http://www.example.com/page.php?topic_id=6f3246d0sdf42c2jb67abba60ce33d5cc
。
区别在于,如果您还没有注意到,在 YouTube 上,他们的观看页面没有文件扩展名,所以我想知道,为什么有些网站不使用文件扩展名,它有什么用处?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
不使用文件扩展名是因为 URI(以及 URL)应该独立于实现 - 如果您想访问 CDC 有关食品安全的信息,您应该能够访问 https://www.cdc.gov/foodsafety(例如)。 CDC 的服务器是否使用 PHP、Python 或 Perl 对于最终用户来说并不重要,因此他们不需要看到它。最终用户并不关心页面是如何生成的,因为提供网页的所有语言都会输出相同的 HTML、CSS 等,并且用户只是在其 Web 浏览器中查看页面。
大多数 Web 框架默认构建此功能,正是出于这个原因,并且无论大多数 Web 服务器中是否进行 URL 重写,它都可以完成。这个理想被编入了 W3C 风格指南,这无疑是这个想法被广泛接受的一个重要因素。他们的指南“Cool URIs Don't Change”中对此进行了概述,其中如果您仍然不太明白这里的推理,应该把事情弄清楚。该文件是该问题的首选声明,也是框架的事实上的标准。
值得注意的是,通常最终下载的文件(有时是 AJAX 中使用的数据文件)的文件扩展名仍然完好无损 - http://example.com/song.mp3 或 http://example.com/whitepaper.pdf - 因为它们旨在保存到最终用户的计算机上,其中文件扩展名很重要。对于仅显示的页面(即大多数页面),不包含扩展名。
后记:这个答案最初链接到的示例页面在某个时候停止存在,因为尽管有最佳实践,但有时 URI 确实会发生变化。我已将其替换为 CDC 的食品安全页面,该页面以某种形式存在于 至少 20 年了。毫无疑问,多年来,许多不同的技术都提供了这些内容,但总是在完全相同的 URL 上提供这些内容。
File extensions are not used because of the idea that URIs (and therefore URLs) should be independent of implementation - if you want to access the CDC's information about food safety, you should be able to go to https://www.cdc.gov/foodsafety (for example). Whether the CDC's servers are using PHP or Python or Perl doesn't matter to the end-user, so they don't need to see it. The end-user doesn't care how the page was generated, because all languages serving a webpage output the same HTML, CSS, and the like, and the user is just viewing the page in their web browser.
Most web frameworks build this functionality in by default, precisely for this reason, and it can be accomplished regardless with URL rewriting in most webservers. This ideal is codified in the W3C Style Guide, which is undoubtedly a big factor in this idea being so widely accepted. It's outlined in their guide, "Cool URIs Don't Change", which should clear things up if you still don't quite understand the reasoning here. That document is the go-to statement on the issue, and the de facto standard for frameworks.
It is worth noting that usually files that end up being downloaded (and sometimes data files used in AJAX) will still have their file extensions intact - http://example.com/song.mp3 or http://example.com/whitepaper.pdf - because they are intended to be saved to the end-user's computer, where file extensions matter. The extensions are not included for pages that are simply displayed - which is most pages.
A postscript: The example page this answer originally linked to stopped existing at some point, because sometimes URIs do change, despite best practices. I've replaced it with the CDC's food safety page, which has existed in some form for at least 20 years now. Undoubtedly, numerous different technologies have served up that content over the years, while always doing so at the exact same URL.
您所看到的是 URL 路由的示例。服务器不是指向特定文件(例如 page.php),而是使用路由表或配置将请求定向到实际呈现 html(或任何其他内容,具体取决于返回的 mime 类型)的处理程序。如果您注意到,StackOverflow 使用相同的机制。
What you are seeing is an example of URL routing. Instead of pointing to a specific file (e.g. page.php), the server is using a routing table or configuration that directs the request to a handler that actually renders the html (or anything else depending on the mime type returned). If you notice, StackOverflow uses the same mechanism.
有或没有扩展名无关紧要。浏览器作用于服务器返回的 MIME 类型,而不是 URL 中使用的任何扩展名。
Having or not having the extension is irrelevant. The browser acts on the MIME type returned by the server, not any extension used in the URL.
当你问“为什么?”您问的是技术原因还是设计原因?有些人已经回答了技术问题,所以我只评论设计。
基本上可以归结为 url 是一个端点。这是用户/服务需要到达的地方。在大多数情况下,扩展名是无关紧要的。如果用户正在浏览网页并访问 http://site.com/users,他期望看到以下列表:用户。他不在乎它没有说 .html 或 .php。作为一名设计师,使用这些扩展并没有真正的意义。您希望您的应用程序有意义,而这些扩展并没有真正提供用户所需的任何洞察力。
当您正在创建其他应用程序将使用的服务时,您可能会想要使用它们。然后,您可以选择使用扩展名来表示期望返回的数据类型(.json、.xml 等)。有人正在研究这些东西的设计指南和规范,但这一切都还很早。
基本上使用这些扩展是因为这就是 Web 服务器/客户端默认的工作方式。随着网络开发的成熟,我们开始更专业地处理网址,并试图让它们对阅读/使用它们的人有意义。
When you ask 'Why?' are you asking for a technical reason or a design reason? Some people already answered the technical so I'll just comment on the design.
Basically it boils down to that url is an endpoint. It's a place that users/services need to get to. The extension is irrelevant in most cases. If a user is browsing the web and goes to http://site.com/users he is expecting a list of users. He doesn't care that it doesn't say .html or .php. And as a designer using those extensions doesn't really make sense. You want your app to make sense, and those extensions aren't really providing any insight that the user needs.
Times that you would want to use them were if you were creating a service that other applications would use. Then you could choose to use an extension to denote what kind of data one could expect to get back (.json, .xml, etc). There are people working on design guidelines and specs for this stuff, but it's all early
Basically those extensions are used because that's how web servers/clients worked by default. As web development has matured we started treating urls more professionally and tried to make them make sense to people reading/using them.
虽然扩展对浏览器来说并不重要,浏览器只是使用传递给它的标头来确定要显示的内容以及如何显示它,但它们很可能在服务器上起作用。例如,您的机器可能同时安装了 php 和 ruby 解释器,但您的网络服务器具有将文件扩展名映射到 MIME 类型的配置文件。例如,来自 Apache 的 php5.conf:
它告诉 Apache 以 .php、.phtml 和 .php3 结尾的文件应该被识别为 PHP 文件。
然而,由于扩展对客户端来说没有任何意义,因此没有它们的 URL 通常看起来“更好”。为了做到这一点,诸如 Apache 的
mod_rewrite
< /a> 可用于“重写”客户端 URL,使其在服务器上有意义。例如,您可以设置
mod_rewrite
规则来重写类似http://yourblog.com/article/the-article-you-wrote
的 URL(看起来更好并且更方便)更容易输入和记住)到http://yourblog.com/articles.php?title=the-article-you-wrote
,Apache 可以使用它来将请求正确路由到您的 PHP 脚本。While extensions don't matter to the browser, which just uses the headers passed along to it to determine what to display and how to display it, chances are they do matter on the server. For instance, your box could have both a php and a ruby interpreter installed, but your webserver has configuration files to map file extensions to MIME types. For instance, from Apache's php5.conf:
which tells Apache that files ending in .php, .phtml and .php3 should be recognized as being PHP files.
However, since the extensions don't mean anything to the client, URLs often look "nicer" without them. In order to do so, technologies such as Apache's
mod_rewrite
can be used to "rewrite" client-land URLs to have meaning on the server.For instance, you could set up
mod_rewrite
rules to rewrite a URL likehttp://yourblog.com/article/the-article-you-wrote
(which looks nicer and is simpler to type and remember) tohttp://yourblog.com/articles.php?title=the-article-you-wrote
, which Apache can use to properly route the request to your PHP script.关键是 HTTP 响应标头的
Content-Type
字段。类似的东西:另请参阅:
更多详细信息:http://en.wikipedia.org/wiki/MIME< /a>
The key is the HTTP response header's
Content-Type
field. Something like that:See also:
More details: http://en.wikipedia.org/wiki/MIME
好吧,文件扩展名在互联网上没有任何用处。浏览器不关心文件扩展名是什么。您可以提供 .avi 形式的 CSS 文件。那么为什么不干脆把它忽略掉呢?这允许更短的 URL。
此外,“重写” url 可以使 url 更具可读性。您可能不理解
/categories.php?id=455
,但您会理解/455-some-category
。如果您想自己执行此操作并且正在使用 Apache,请查看 mod_rewrite。
Well, file extensions aren't of any use on the internet. The browser doesn't care what the file extension is. You could serve a CSS file as .avi. So why not simply leave it out? This allows for shorter URLs.
Furthermore "rewriting" a url allows for more readable urls. You may not understand
/categories.php?id=455
but you do/455-some-category
.If you want to do this yourself and are using Apache have a look at mod_rewrite.
url 应该被正确地视为用户界面的一部分。因此,它的设计应该能够传达有关用户在网站上的位置以及网站结构的信息。
之类的 URL
诸如mysite.com/sport/soccer/brazil_wins_worldcup
会告诉用户很多有关该网站的结构以及他当前所在位置的信息。相比之下:
mysite.com/article.php?cateogry=12&articleid
=371 是无用的,相反,它暴露了不相关的实现细节,例如使用哪种语言来创建网站,以及该文章的 id 是什么(可能存储在在该 id 下的数据库中)
除了这种审美观点(不要让用户接触不相关的实现细节)之外,它还有助于使网站面向未来。因为如果您一开始就没有公开过您选择的语言,那么您可以稍后升级到 Ruby 或 Python,而无需世界上每个链接都指向您,现在都是 404。
设计对用户有意义的 URL, 面向未来。
The url, should properly be considered part of the user-interface. As such, it should be designed to convey information about where the user is on the site, and the structure of the site.
A url such as:
mysite.com/sport/soccer/brazil_wins_worldcup
tells the user a lot about the structure of the site, and where he currently is. In contrast:
mysite.com/article.php?cateogry=12&articleid=371
is useless, instead it exposes irrelevant implementation-details such as which language is used to make the site, and what the id of that article is (likely stored in a database under that id)
In addition to this estethical argument (don't expose the user to irrelevant implementation-details) it also helps with making the site future-proof. Because if you never exposed your language of choice to begin with, you can later upgrade to Ruby or Python, without every link in the world that points to you, now being a 404.
Design urls to make sense for users, and to be future-proof.
这个问题有很多可能的答案。您的 Web 应用程序服务器的配置方式决定了您的 Web 浏览器所解释的内容。在某些情况下,您可能会使用 URL 重写或路由,并且正如其他人所说,您为请求的 URL 或扩展提供了哪些处理程序。
我可以有一个像“http://cory.com/this/really/doesnt/exist 这样的网址”并让它实际指向“http://cory.com/this .does.exist.123”如果我愿意的话。
There are many possible answers to this. It's how your web application server(s) are configured that results in what your web browser is interpreting. There could be situations where you're using URL rewriting or routing, and as others have said, what handlers you're providing for requested URLs or extensions.
I could have a URL like "http://cory.com/this/really/doesnt/exist" and have it actually be pointing at "http://cory.com/this.does.exist.123" if I wanted to.
Web 服务器的正常行为是将请求的 URI 路径映射到文档根目录中某处的文件上。因此,
http://example.com/foo/bar
简单地映射到/path/do/document/root/foo/bar
。此外,Web 服务器需要知道如何处理文件。这通常是通过文件扩展名来完成的。因此文件扩展名.php
的文件由 PHP 解释器处理。现在,除了这种正常行为之外,大多数 Web 服务器都具有允许更改映射的功能(即 URL 重写< /a>)以及如何处理没有文件扩展名的文件。
对于 Apache Web 服务器,前者可以使用 mod_rewrite:
后者可以通过 mod_mime 完成:(
好吧,实际上这不是 mod_mime 功能,而是一个 核心 功能。)
The normal behavior of a web server is to map the requested URI path onto a file somewhere in the document root directory. So
http://example.com/foo/bar
is simply mapped onto/path/do/document/root/foo/bar
. Additionally, the web server needs to know how to handle a file. This is often done by the file name extension. So files with the file name extension.php
are handled by the PHP interpreter.Now apart from this normal behavior, most web servers have features that allow to change both the mapping (i.e. URL rewriting) and the way how a file without a file name extension is handled.
In case of the Apache web server, the former can be done with mod_rewrite:
And the latter can be done with mod_mime:
(Ok, actually this is not mod_mime feature but a core feature.)
规则:URI 中不应包含文件扩展名
在 Web 上,句点 (.) 字符通常用于分隔文件名和文件名。
URI 的扩展部分。 REST API 不应包含人工文件扩展名
在 URI 中指示消息实体主体的格式。相反,他们应该依靠
通过 Content-Type 标头传达的媒体类型,以确定如何
处理身体的内容。
(1)http://api.college.restapi.org/学生/3248234/transcripts/2005/fall.json
(2)http://api.college.restapi.org/students/ 3248234/transcripts/2005/fall
(1)文件扩展名不应用于指示格式偏好。
(2)应鼓励REST API客户端使用HTTP提供的格式选择
机制,Accept 请求标头。
参考资料:设计 REST api 规则手册
Rule: File extensions should not be included in URIs
On the Web, the period (.) character is commonly used to separate the file name and
extension portions of a URI. A REST API should not include artificial file extensions
in URIs to indicate the format of a message’s entity body. Instead, they should rely on
the media type, as communicated through the Content-Type header, to determine how
to process the body’s content.
(1)http://api.college.restapi.org/students/3248234/transcripts/2005/fall.json
(2)http://api.college.restapi.org/students/3248234/transcripts/2005/fall
(1)File extensions should not be used to indicate format preference.
(2)REST API clients should be encouraged to utilize HTTP’s provided format selection
mechanism, the Accept request header.
references: design REST api rulebook
下面是我在 .htaccess 中使用的内容,使 url 在没有 HTML 或 PHP 扩展名的情况下仍然可以正确运行。
意味着如果浏览器中具有指定名称的文件与网络服务器中的目录(-d)或文件(-f)不匹配,则重写下面的规则
我不确定下面的规则如何工作,但我认为之后它用html重写,如果仍然不匹配,那么用php重写,
如果仍然不匹配,则会显示404页面。
您还可以使用 .htaccess 中的以下代码重定向 404,
重要性是该代码适用于我的网站。
http://mintnet.net/services
http://php.mintnet.net/home
这些不需要文件扩展名。
below it what I use in my .htaccess to make the url still run correctly without the HTML or PHP extension.
means that if the file with the specified name in the browser doesn't matching with directory (-d) or files(-f) in your webserver, then rewrite rule below
i'm not sure how the below work but I think that after it rewrite with html and if it still not matching it then rewrite with php
if it still not matching it will be show 404 page.
you also can redirect 404 with the code below in .htaccess
importance is the code is working in for my site.
http://mintnet.net/services
http://php.mintnet.net/home
those doesn't need the file-extension.
“www.youtube.com/watch”是 YouTube 的目录。所以它基本上可以写成“www.youtube.com/watch/”,并以正斜杠结尾。
"www.youtube.com/watch" is a directory of YouTube. So it can basically be written as "www.youtube.com/watch/" with the ending forward slash.