How does HTTPS work?

In short
HTTPS is HTTP with an encryption layer added. HTTP is the protocol browsers and servers use to exchange requests and responses. HTTPS keeps that same request and response pattern but protects the messages while they travel across the network, so computers in between cannot easily read them.
What HTTPS means
HTTPS is the secure form of HTTP, the Hypertext Transfer Protocol. A protocol is a set of agreed rules that two programs follow when they talk. HTTP defines how a client asks for something and how a server answers. HTTPS follows the same rules, but it wraps the conversation in encryption. Encryption scrambles data so that only the two ends can read it.
When you use HTTPS, your browser still sends a request and the server still sends a response. The difference is what happens to those messages on the way. They are protected while they cross the network.
The parts involved
HTTP is a client-server protocol. The client is usually a web browser, acting on behalf of a user. A client can also be a program, such as a robot that crawls the web or a tool a developer uses to test an application. The client always starts the conversation. The server waits for requests and returns a response.
Between the client and the server there are often other machines called proxies. A proxy relays messages and may cache them, filter them, balance load between servers, check access, or keep logs. Some proxies pass messages along untouched. Others change them before forwarding. Many devices on the path, such as routers, sit below the HTTP layer and do not look at the content of a request.
HTTPS matters most here. Without encryption, every proxy and network device on the path could read the messages. With HTTPS, the content is protected, so only the client and the server can read it.
How a request looks
HTTP messages are designed to be readable by people. A simple request for a page looks like this:
GET / HTTP/1.1 Host: developer.mozilla.org Accept-Language: fr
The first line names the method and the path. The header lines add extra information, such as which site the request is for and which language the client prefers. The server replies with a status line, headers and the body of the page.
With HTTPS, this same text is sent through an encrypted channel. You do not change your code to read the page. The browser or your tool handles the encryption for you.
Connections and setup cost
HTTP relies on a transport protocol to deliver its messages. The common choice is TCP, which is reliable and connection-based. A client and server must first open a TCP connection, and this takes several round trips before any request can be sent.
HTTPS adds a further setup step on top of this. Before any page is requested, the two ends agree on how they will encrypt their data. This extra exchange means the first connection takes longer. Once it is open, the connection can be reused for more requests, which helps keep the cost down.
The documentation we used for this page describes plain HTTP and its connections. It does not explain the details of encryption. For the exact steps of the secure handshake, read the documentation for the security protocol that HTTPS uses.
Why HTTP is stateless
HTTP is stateless. Each request stands alone, and the server does not remember the one before it. This makes a site hard to use for things like a shopping basket. Cookies solve this. A cookie is a small piece of data that the server asks the browser to store and send back with later requests. Together, cookies let a site build a session on top of stateless HTTP.
HTTPS does not change this. Cookies still work the same way over an encrypted connection. A secure site usually marks its session cookies so they are sent only over HTTPS.
Common mistakes
A frequent mistake is to assume that HTTPS makes a whole site safe. It only protects data in transit between the client and the server. It does not check the code on the server or the data stored there. Each part still needs its own care.
Another mistake is to mix secure and plain pages. If a secure page loads scripts or images over plain HTTP, the browser may block them or warn the user. Load every resource over HTTPS.
Developers also sometimes forget that proxies can sit in the path. A caching proxy that stores responses must be set up so it does not mix up private content. HTTP headers tell caches what they may store and for how long.
Where to read more
The official HTTP overview on MDN explains the client, the server, proxies and the request flow in detail. It is the best place to continue after this page. Start with the parts of the flow you find most unfamiliar, then return to the security details when you are ready.
FAQ
Is HTTPS the same as HTTP?
No. HTTPS uses the same request and response rules as HTTP, but it adds encryption so that the messages cannot be easily read while they travel across the network.
Does HTTPS make a website completely secure?
No. HTTPS protects data between the browser and the server. It does not fix weak code on the server or protect data once it is stored there.
Why is the first HTTPS connection slower?
Before a request is sent, the client and server must open a connection and agree on encryption. This setup takes extra time, but the connection can then be reused for more requests.
Can proxies read HTTPS traffic?
Proxies that only relay encrypted traffic cannot read its content. Their role is limited to passing the messages along, so the protection depends on the encryption working correctly end to end.
Sources
This page is based on the official documentation below. Read it for full detail.
HTTPS — official documentation ↗