大网数据广告活动
网站运营与故障问答

Best form for URLs: file-name.html or /file-name/

· 52CE 我爱测

Running a Nibbler analysis on a static website I just built, I got the following feedback: Avoid use of file extensions wherever possible. File extensions appear at the end of web addresses, and have several negative effects. The…

问题原文

原帖链接 · Jim · CC BY-SA 4.0

Running a Nibbler analysis on a static website I just built, I got the following feedback:

Avoid use of file extensions wherever possible. File extensions appear at the end of web addresses, and have several negative effects. They make the address harder to remember or type (particularly for non-technical users), and can reveal the underlying technology of the website making it very slightly more vulnerable to hackers. They also tie the implementation of the website to a specific technology, which can make subsequent migration of URLs difficult.

The above message is a result of having a flat directory structure and linking directly to the individual web pages (whatever.html). So is it really that much better to put every html file on a website in its own personal subdirectory (giving every webpage the file name index.html and relying on the directory name to identify the file) vs simply linking directly to the individual html files? I did some searching but didn't really find anything useful. This discussion had some info but didn't answer my question.

I'm curious to know what folks think of the two different approaches. Thanks.

社区高分解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

You have several options for removing extensions. You don't have to create a directory for each file.

  • Use Apache's mod_negotiation with multiviews. You enable it with Options +multiviews in your .htaccess file. Then you can link to <a href="some-page"> and Apache will find some-page.html and serve it. Same goes for <img src="some-image"> for which Apache will automatically serve some-image.png, some-image.jpg, or some-image.gif if any of them exist.
  • You can save your files without a file extension. However that introduces other problems: Its difficult to configure your server to serve the correct content type for different files and it will make editing the files harder for you.
  • Use mod_rewrite to remove extensions from some files.
  • Use a front controller to serve your entire site from one script that does its own content mapping based on the URL. That is how most content management systems such as WordPress work.

Before you go and do any of that, I'd suggest that you read Jesper M's answer to the question that you linked. Having the extension match the mime type can be beneficial when somebody saves something from your site locally. If your page gets saved locally as some-page.html it will likely work correctly but if it gets saved as just some-page without an extension, the user may not be able to open the file. I'd recommend removing extensions from the URL that indicate what scripting language you are using (.php, .cgi, .asp, etc) but not necessarily removing extensions that indicate the file type (.html, .png, .jpg, etc.)

As far as SEO goes, it doesn't matter if you have file extensions or not. Google doesn't give much (if any) weight to what your URL looks like right now. I'd focus on how your URLs impact usability: descriptive, reasonably short, and maybe a file extension that matches the file type.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-09-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。