Modern Internet is built on communication between the client (browser) and the server. So that they can exchange information with each other, special protocols have been developed. One of these is HTTP, and thanks to it, users can work through a browser and view HTML pages.
What are HTTP headers
HTTP is a way to share HTML pages between two computers. The protocol was invented in 1990 and is currently the primary method for displaying hypertext pages.
HTTP headers are the strings that computers communicate with. It is like a dialogue between people. When the website is opened, the browser generates a request, it indicates the necessary information about itself (language, country, link to the resource, kernel version, etc.). All this information is sent to the server, and there is a certain program (Apache, Nginx, LiteSpeed, etc.). She reads the received lines and, depending on the question, generates an answer.
For example, a person decided to open google.com, he enters a link in the search bar and the browser generates a request. The browser HTTP header conditionally looks like this:
Start line | Show google.com |
Inquiry | I'm from Russia I use Google Chrome I need HTML I have user information |
HTTP body | Awaiting |
The server processes the data and generates a response:
Start line | Everything is fine, the page is found and works |
Answer | I am running Apache Page modified on 05/27/2017 UTF-8 encoding |
Message body | Get page code This is new information from the user (login, password) |
The body of the message conveys the HTML code of the page.
HTTPS Features
Now most sites are switching from an HTTP to an HTTPS connection. The difference between the latter is the additional encryption of all transmitted information. Before starting the exchange, the client requires an SSL certificate to authenticate the server. For the developer, nothing changes, and he can continue to work without changing his code.
After receiving the certificate, the client checks it for authenticity (the certificate from the server and the certificate from the center are compared). If all is well, then the HTTP protocol is launched. After the certificate is confirmed, the headers are encrypted through RSA. Now the attacker will not be able to steal important user information (login, password, etc.).
View HTTP Dialog
You can view the HTTP dialog yourself. Special abbreviations are used as phrases - Date, Cookie, Host, Server, etc. You can view HTTP headers using browser extensions. Online services will also help.
To view HTTP headers from plugins use:
- Firebug
- Live HTTP Headers.
- HTTP headers.
From online services use:
- Bertal.ru.
- stupid.su.
- Speed-Tester.Info.
They intercept the headers received from the server and display them in a separate window. Moreover, from one page you can immediately receive 100-200 headings, and they can be sent periodically after some time. For example, to check online in social networks.
HTTP headers can be divided into four types:
- General (General headers) - used in the request and response;
- for request (Request headers) - for request;
- for response (Response headers) - for answers;
- Entity headers - Requests and responses.
Starting line from the client
To begin the exchange of information, the main line is first formed. The client in it says which file or object it needs. For this, certain methods of accessing the protocol are used. The structure of the start line consists of three parts:
Request method | An object | protocol |
Post | /c840024/upload.php | HTTP / 1.1 |
After this, the Host line is required and the URL of the site is indicated. There are different query methods. Programmers most often use:
- GET - request for information (the response is sent by the server in the link).
- POST - sending information to the server in a hidden way (the answer is not visible in the address bar).
- HEAD is the same as GET, but the server will only return the header.
- PUT - transfer of large requests to URL;
After sending the start line, all other headers follow - User Agent, Cookie, etc. Without an initial call, it is impossible to start exchanging information via HTTP. Headings are only an addition and in protocol 1.0 they may not be transmitted at all.
Information transfer from the client
After the initial line is transmitted, the client sends its data to the hosting, for example, the browser version and language used. If desired, the server can additionally request other information from the client:
Mandatory (always transmitted) HTTP request headers are Host, Referer, User Agent, and Accept.
The programmer cannot affect the request headers, they are formed by the browser. You can configure their transfer in the program itself through additional extensions.
Server HTTP headers - response to page requests
After receiving requests from the client, the page passes certain lines to the server. In php, the header () function is used to pass an HTTP header. For example, you can report a new page location:
header ('Location: http://www.site.com/').
From the page on the server, data with the necessary information is sent to the client. In this way, you can find the necessary information from the server:
Mostly these queries are needed for the page to display correctly in the browser. They are used to improve page loading speed.
Entity Headers
There are headers that imply a request and response, each request is attached to a specific entity (page with HTML code). Thanks to these requests, the browser updates the page information. They are actively used in caching.
The most popular entity header is Last-Modified. This request can be sent both from the browser to the server, and vice versa. Through this header, the client finds out if it needs to update its cache. Dialog example :
Client: "I have a cache dated 05/16/2016, has the page changed on the server?"
Server: "Yes, the cache changed on 03/19/2017, here is the new version."
Server response
After receiving the start line from the client, the server generates its response.
HTTP | Protocol version | Status Header | explanation |
HTTP | 1.1 | 200 | Ok |
If the connection status is confirmed, the server can send the requested information to the client.
An example of http-dialogue can be seen in the picture below.
Requests are generated by the programmer on the page using the header () function.
Status Codes
To continue communication, the client needs to be sure that everything works on the hosting and is displayed correctly. To make sure of this, the statuses of the answers were invented. They are a three-digit number.
You can transfer the status from the page as the initial heading, for example, header ("http / 1.1 200 Ok").
Cached pages
To speed up the process of sharing pages, caching was invented. The page is saved in compressed form to the local storage of the computer. Now the headers do not need to send large files every time. You only need to make sure that the information on the hosting and the client is the same.
Special cache request lines have been created, after receiving headers from the client, the hosting checks if it has a cache for the page. If it is not, then it requests it from the server. In the future, before accessing the browser, the protocol will only check if the cache on the server has changed.
To check the compressed information for relevance, the expiration date is indicated in the HTTP headers. The client sends information about which files are stored locally, and the server indicates its version. If they match, then the browser simply displays its version of the cache.
For SEO optimization, you must specify the date in the HTTP headers. For these purposes, Last-Modified is used. In addition, the cache can be updated after some time storage. Expires is used for this. Cache-Control is used to configure caching. Thanks to it, you can enable or disable the storage of information from the page.
The correct caching setting allows you to quickly promote your resources to the top SERPs for search engines. Algorithms Yandex and Google periodically visit the site’s pages and store its cache in their archives. After some time, they contact the server to check the relevance of the information. If the information has been changed, then the files on the search engine server are updated, and the received data is reindexed.
Some people advise passing the current date in the Last-Modified heading, hoping that because of this the robot will constantly keep their article at the top of the search results. But it turns out that each time the algorithm has to change the cache information inside itself and reindex it again. This does not always lead to good results and sometimes can only worsen the position of the article.
About the URL in the browser bar
To communicate with the hosting, the user each time enters a URL link into the address bar of the browser. This character set has a certain structure, and the necessary information is transmitted through it. The link structure consists of five parts:
- protocol;
- the object of interest and its address;
- port for circulation;
- HTTP strings (when sent using the GET method);
- query code.
Using links, you can access not only pages such as HTTP, PHP, etc. Through them, you can search the databases or send information to another computer. In this way, hackers often perform SQL injections and steal information from the site’s databases using various methods.
The disadvantage of the URL is the lack of support for other alphabets - mainly Latin is used. Because of this, you need to correctly think about the abbreviated title of the article before publication. After all, the search engine by the link evaluates the usefulness of the resource and the information that the page can provide for the user. Therefore, with SEO optimization, special attention should be paid to the formation of the correct URL for the article.