33

I was browsing the internet and noticed, YouTube, for example, contains a URL like this to denote a video page: http://www.youtube.com/watch?v=gwS1tGLB0vc.

My site uses a URL like this for a topic page: http://www.example.com/page.php?topic_id=6f3246d0sdf42c2jb67abba60ce33d5cc.

The difference is, if you haven't already noticed that on youtube, there is no file extension for their watch page, so I am wondering, why do some sites not use file extensions and what use does it serve?

4

13 回答 13

38

不使用文件扩展名是因为 URI(因此 URL)应该独立于实施 - 如果您想访问 CDC 有关食品安全的信息,您应该能够访问https://www.cdc.gov /食品安全(例如)。CDC 的服务器是使用 PHP 还是 Python 还是 Perl 对最终用户来说并不重要,因此他们不应该看到它。最终用户并不关心页面是如何生成的,因为为网页提供服务的所有语言都输出相同的 HTML、CSS 等,而用户只是在他们的 Web 浏览器中查看页面。

大多数 Web 框架默认构建此功能,正是出于这个原因,并且无论在大多数 Web 服务器中使用 URL 重写,它都可以实现。这个理想被编入 W3C 风格指南,这无疑是这个想法被广泛接受的重要支持者。在他们的指南“Cool URIs Don't Change”中对此进行了概述,如果您仍然不太理解这里的推理,这应该可以解决问题。该文件是有关该问题的首选声明,也是框架的事实上的标准。

值得注意的是,通常最终被下载的文件(有时是在 AJAX 中使用的数据文件)仍然具有完整的文件扩展名 - http://example.com/song.mp3http://example.com/whitepaper .pdf - 因为它们旨在保存到最终用户的计算机,文件扩展名很重要。对于仅显示的页面(大多数页面)不包括扩展名。

后记:这个答案最初链接到的示例页面在某些时候停止存在,因为尽管有最佳实践,但有时 URI 确实会发生变化。我已将其替换为 CDC 的食品安全页面,该页面以某种形式存在至少 20 年了。毫无疑问,多年来,许多不同的技术都提供了该内容,同时始终使用完全相同的 URL。

于 2010-09-02T21:03:59.950 回答
14

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.

于 2010-09-02T20:55:03.117 回答
7

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.

于 2010-09-02T20:53:09.597 回答
6

当你问“为什么?” 你问的是技术原因还是设计原因?有些人已经回答了技术问题,所以我只会对设计发表评论。

基本上它归结为那个 url 是一个端点。这是用户/服务需要到达的地方。在大多数情况下,扩展是无关紧要的。如果用户正在浏览网页并访问http://site.com/users,他期待的是用户列表。他不在乎它没有说 .html 或 .php。作为使用这些扩展的设计师并没有真正的意义。您希望您的应用有意义,而这些扩展并没有真正提供用户需要的任何洞察力。

如果您正在创建其他应用程序将使用的服务,那么您希望使用它们的时间。然后,您可以选择使用扩展名来表示期望返回的数据类型(.json、.xml 等)。有人在为这些东西制定设计指南和规范,但这一切都还为时过早

基本上使用这些扩展是因为这是默认情况下 Web 服务器/客户端的工作方式。随着 Web 开发的成熟,我们开始更专业地处理 url,并试图让它们对阅读/使用它们的人有意义。

于 2010-09-02T21:01:02.557 回答
5

虽然扩展对浏览器无关紧要,浏览器只是使用传递给它的标题来确定要显示什么以及如何显示它,但它们很可能在服务器上很重要。例如,您的机器可能同时安装了 php 和 ruby​​ 解释器,但您的网络服务器具有将文件扩展名映射到 MIME 类型的配置文件。例如,来自 Apache 的 php5.conf:

  AddType application/x-httpd-php .php .phtml .php3

它告诉 Apache 以 .php、.phtml 和 .php3 结尾的文件应该被识别为 PHP 文件。

然而,由于扩展对客户端没有任何意义,没有它们的 URL 通常看起来“更好”。为此,可以使用诸如 Apache 之类的技术mod_rewrite来“重写”客户端 URL,使其在服务器上有意义。

例如,您可以设置规则将 URL (看起来更好,更易于键入和记住)mod_rewrite重写为,Apache 可以使用该规则将请求正确地路由到您的 PHP 脚本。http://yourblog.com/article/the-article-you-wrotehttp://yourblog.com/articles.php?title=the-article-you-wrote

于 2010-09-02T21:07:58.903 回答
2

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.

于 2011-04-29T12:20:40.070 回答
2

The key is the HTTP response header's Content-Type field. Something like that:

HTTP 200 OK
Content-Type: video/flv
Content-Length: 102345

DATA-DATA-DATA-DATA-DATA-DATA-....

See also:

Content-Disposition: attachment; filename=genome.jpeg;
     modification-date="Wed, 12 Feb 1997 16:29:51 -0500";

More details: http://en.wikipedia.org/wiki/MIME

于 2010-09-02T20:52:51.483 回答
2

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.

于 2010-09-02T20:58:05.437 回答
1

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.

于 2010-09-02T20:56:35.047 回答
1

Web 服务器的正常行为是将请求的 URI 路径映射到文档根目录中某处的文件。所以http://example.com/foo/bar简单地映射到/path/do/document/root/foo/bar. 此外,Web 服务器需要知道如何处理文件。这通常由文件扩展名完成。因此,带有文件扩展名的文件.php由 PHP 解释器处理。

现在除了这种正常行为之外,大多数 Web 服务器都具有允许更改映射(即URL 重写)和处理没有文件扩展名的文件的方式的功能。

对于 Apache Web 服务器,前者可以使用mod_rewrite完成:

RewriteEngine on
RewriteRule ^/watch$ /watch.php

后者可以用mod_mime完成:

<File watch>
    ForceType application/x-httpd-php
</File>

(好吧,实际上这不是 mod_mime 功能,而是核心功能。)

于 2010-09-02T21:01:29.093 回答
0

下面是我在 .htaccess 中使用的内容,以使 url 在没有 HTML 或 PHP 扩展名的情况下仍能正常运行。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f

表示如果浏览器中指定名称的文件与您的网络服务器中的目录(-d)或文件(-f)不匹配,则重写下面的规则

RewriteRule ^(.*)$ $1.html

我不确定下面是如何工作的,但我认为在它用 html 重写之后,如果它仍然不匹配,那么用 php 重写

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

如果仍然不匹配,则会显示 404 页面。

您还可以在 .htaccess 中使用以下代码重定向 404

ErrorDocument 404 /404.html

重要的是代码正在为我的网站工作。

http://mintnet.net/services

http://php.mintnet.net/home

那些不需要文件扩展名。

于 2017-04-04T19:47:18.270 回答
0

规则:文件扩展名不应包含在 URI 中

在 Web 上,句点 (.) 字符通常用于分隔 URI 的文件名和扩展名部分。REST API 不应在 URI 中包含人为的文件扩展名来指示消息实体主体的格式。相反,它们应该依赖于通过 Content-Type 标头传达的媒体类型来确定如何处理正文的内容。

(1) http://api.college.restapi.org/students/3248234/transcripts/2005/fall.json (2) http://api.college.restapi.org/students/3248234/transcripts/2005/fall

(1)不应使用文件扩展名来表示格式偏好。(2) 应鼓励 REST API 客户端使用 HTTP 提供的格式选择机制,即 Accept 请求标头。参考:设计 REST api 规则手册

于 2015-12-18T08:50:07.023 回答
-4

“www.youtube.com/watch”是 YouTube 的目录。所以它基本上可以写成“www.youtube.com/watch/”结尾的正斜杠。

于 2011-05-30T23:24:06.410 回答