I’m learning HTML, and I can’t figure out why some websites don’t have a .html extension on their webpage.
Example: no html extension
Example: html extension
Anyone know how I can make it look like that there is no extension? Is it safer than the other method?
I tried creating a file without an html extension, but it only showed text instead of html.
Thanks for any help!
5
Answers
By default, every HTML file has to have the correct extension like
example.html
to be able to be rendered by the browser.If you remove the extension and open it in your browser using file protocol (
file://.../example
) it will only show its content as text, without rendering it as an HTML file.To render an HTML file without needing the extension, you need to serve it through an HTTP server. This server can be created using back-end technologies like PHP, Node.js, Python Frameworks, C++, Java, and Java Frameworks such as Spring Boot, etc. When serving your HTML files through a HTTP server you can define which URL serves what content. For example when a request hits the URL
http://localhost/example
you can write code to respond to this request with an HTML file, yourexample.html
file. This way, the browser will show a URL ofhttp://localhost/example
but it (the server) will serve you with an HTML file.When you visit a webpage (which is different from simply just opening an .html file on your computer), the server looks at the URL you gave it in the address bar, and sends your browser some chunk of text based on that URL. If the response from the server is intended to be an HTML page, then the server will include along with the response a header that tells your browser that the text it just received needs to be processed and displayed as a .html file.
Because a server is a computer program, and web developers have incredible freedom when developing a website, the server can send you anything it wants when you give it a URL. You can program a website to send back an image of a donkey when you give it a URL of /hello.html if you want to.
Some web developers give all the URLs on their site a .html extension for various reasons, some do it for old search engine optimization (modern search engines don’t care), and some simply do it because of preference.
The reason your browser requires a .html extension when you open a .html file on your computer is that otherwise, the browser won’t know what kind of text it is. As mentioned before, web servers send along a “header” with the response to your browser when you visit a page on the internet so including the .html extension in the URL is unnecessary.
In general, the presence or absence of file extensions in website URLs serves different purposes:
With extensions (e.g., ".html"):
Without extensions:
The URL ending in .html doesn’t necessarily mean it was a static HTML page, since a rewrite rule could make any dynamic content also end w/ an extension of ".html".
Also as @user3163495 mentions, you can also check the header information for additional details.
That is because most websites these days use a router (usually with a framework).
Modern websites need a database, authentication system, and … which can not be done using pure HTML and CSS. For these purposes, we need a server-side language to be able to get data from the database, authenticate users, and … like php language. When we are creating a website using PHP or any other server-side language, we usually like to create a router, which manages URL (website address) like https://example.com/slug1/slug2/...
We call these parts slug. Which are separated by /. And yes, it is not so good to show the name of the file that is hosted on a server like https://example.com/login.html.
So in a standard website, we manage url by a routing system and define slugs and the content that should be shown for each URL. But these days we do not create a routing system ourselves. There are a lot of frameworks like Laravel for PHP, that have built-in routing systems and you just define the URL for each page. It is more standard and meaningful. But if you are creating a simple website using pure HTML, that’s okay. I just wanted to say most websites are created by different frameworks that handle URLs themselves.
That’s why you cannot see the url. When you run an html file in your browser, in the browser address bar, you see the html file address in your local machine, and when you upload host it, the filename and extension will be shown, like example.com/home.html, but as I said modern websites are created using a framework, Laravel, React, Vue and …
One thing not mentioned in the other answers is that you can easily create URLs without an extension (at least on Apache) by calling the file
index.html
orindex.php
(or another name other thanindex
if the server is so configured, such ashome
). So if your home page ismysite.com/index.html
, the URL will just bemysite.com
for your home page. If you wanted the URL to bemysite.com/about/
, you could put your about content in anotherindex.html
file in an/about/
folder. Somysite.com/about/index.html
will show in the browser asmysite.com/about/
.