📜 ⬆️ ⬇️

Did not put favicon on the site - get double traffic from Chrome

For the first time in five years of website development, I faced a very unexpected problem that cost me many hours of searching, nerves and hair on my head. Suddenly, I discovered that on a new site, which I am currently developing on localhost, INSERT queries are being duplicated to the database. I send one comment through the form, and two are inserted into the base. If you do not know how this problem is connected with Chrome, favicon.ico and ModRewrite, then welcome under cat.

No, this is not a double vision


Of course, the first thing that came to mind was that somewhere in the script a database query was “doubled”. For example, such a “school mistake”:

$query = "INSERT INTO table VALUES(...)"; $result = mysqli->query($query); if ($result = mysqli->query($query)) { ... } 

I check one hundred thousand times - no. The code is all right. So the browser for some reason sends the data twice. I generate a simple test script with an increment of the session variable and - yes! Instead of increasing by one, the browser stubbornly shows an increase in the variable by two. I try instead of Chrome Safari - there is no such problem. Next comes the search for incorrectly working javascripts, browser extensions, but to no avail. Search on the Internet gave similar advice. Many programmers in a similar situation introduced a check for uniqueness and cut off duplicate posts. But the bug does not eliminate this decision and I decided not to stop and find the reason for this behavior.

Finally, the reason was found. And since I found the solution with difficulty, on the English-speaking developer branch, I want to share it here. Everything is simple - there are two culprits: Google Chrome (and browsers derived from it) and Apache's mod_rewrite.
')

The essence of the problem


It's simple. The peculiarity of browsers built on the Crome platform is that they search for the favicon.ico file for each site. And if it is not, then they will still persistently look for him. With each page refresh, a separate request to the server. And the majority of .htaccess files in Apache have the lines:

 RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . /index.php 

Guided by this rule, when receiving a request from the browser to a non-existent favicon.ico, Apache dutifully redirects it to the index.php file and the script runs twice. Of course, in most cases, full-fledged web applications have a check for uniqueness and do not miss repeated requests. And sites in most cases have a file favicon.ico. But nevertheless time there is such a problem, it means you can describe the methods of solution.

Solution options


The first solution is the simplest: get on the website favicon.ico. Chrome will find it and calm down.
The second solution is to slightly modify the .htaccess file on the server. My mod_rewrite rule block for all projects will now look like this:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f # Next line is to solve the Chrome and favicon.ico file issue. # Without it browser sends two requests to script. RewriteCond %{REQUEST_FILENAME} !favicon.ico # RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 

Did not put a favicon? Get double traffic from Chrome


If you think about it, then the statement is true, because instead of giving the “static”, the server will launch a web application, which at best will return to the browser instead of the icon a dynamically generated 404 page. And in the worst case, the launch of the main page of the site will completely work. It turns out that without installing an icon on the site (for example, on one popular blog engine), the developer doubles the load on the server from Google Chrome users.

That's all. I hope this information is useful to someone and will save time.

Source: https://habr.com/ru/post/309436/


All Articles