Search code examples
htmlfavicon

favicon doesn't show up unless its name is "favicon.ico" - Local Apache2 Server


I am trying to add a favicon to my web page.

It works fine if the icon name is favicon.ico. But what if I don't want it to be named like that ?

What if I want to add more icons with different sizes. Like a 192x192 for Android Chrome ?

My website isn't live. It's local. It's on Apache2. I have it on a Raspberry Pi and on my Linux PC. Both servers have identical settings/configurations. It behaves exactly the same on both servers.

All files(html,php,png,ico, etc) are inside one directory: /var/www/mysite. I've changed DocumentRoot to DocumentRoot /var/www/mysite inside etc/apache2/sites-enabled/000-default.conf

I am using Chrome. I cleared the cache. I'm refreshing the page using SHIFT + F5, CTRL + F5 or F5; all with similar results.

Note that it's index.php not index.html. Here is what I've written inside the head tag inside index.php:

<head>
    <meta charset = "utf-8">
    <meta name = "viewport" content = "width=device-width, initial-scale=1.0">    

    <!-- Chrome, Firefox OS and Opera -->
    <meta name="theme-color" content="#e11422">
    <!-- Windows Phone -->
    <meta name="msapplication-navbutton-color" content="#e11422">
    <!-- iOS Safari -->
    <meta name="apple-mobile-web-app-status-bar-style" content="#e11422">
    
    <link rel="shortcut icon" href="/favicon16x16.ico?v=3" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="favicon-192x192.png">

    <link rel = "stylesheet" type = "text/css" href = "master.css">   
    <title>Home Page</title>
    <script type="text/javascript" src="jquery-1.12.4.min.js"></script>

<!-- <script>
    $(document).ready( function(){
    $('.text-box').load('events_placeholder.html');
    refresh();
    });
    
    function refresh(){
        setTimeout( function() {
            $('.text-box').load('events_placeholder.html');
            refresh();
        }, 1000);
    }
</script> -->

<script>
    /* When the user clicks on the button,toggle between hiding and showing the dropdown content */
    function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
    }

    /* Close the dropdown menu if the user clicks outside of it */
    window.onclick = function(event) {
    if (!event.target.matches('.dropbtn') && !event.target.matches("#text-input") && !event.target.matches("#button")) {
        var dropdowns = document.getElementsByClassName("dropdown-content");
        var i;
        for (i = 0; i < dropdowns.length; i++) {
        var openDropdown = dropdowns[i];
        if (openDropdown.classList.contains('show')) {
            openDropdown.classList.remove('show');
        }
        }
    }
    }
</script>

<script>
$(document).ready(function() {
    $('.URR').change(function() {
        var n = $('.URR').val();
        if (n < 1)
            $('.URR').val(1);
        if (n > 10)
            $('.URR').val(10);
    });
    $('.Brightness').change(function() {
        var m = $('.Brightness').val();
        if (m < 2)
            $('.Brightness').val(2);            
        if (m > 99)
            $('.Brightness').val(99);
    });
});
</script>

</head>

I've read all that I could find on SO and on Google. The only thing that came close to answering my questions is this SO question. But, the question being 8 years old, I really hope that something has changed since then.

Fix:

The fix I found was to actually include a favicon.ico.

Also, thanks to @Punit Gajjar I was able to find one of the best favicon tools online: Real Favicon Generator.

I recommend it to everyone who's trying to make a favicon. After using the Favicon Generator I've added all the files generated (png, ico) to where index.php is located and also the following code inside the head tag:

    <!-- iOS App Icon-->
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png?v=476mA4zprB">
    <!-- Chrome, Firefox OS and Opera -->
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png?v=476mA4zprB">
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png?v=476mA4zprB">
    <link rel="manifest" href="/site.webmanifest?v=476mA4zprB">
    <link rel="mask-icon" href="/safari-pinned-tab.svg?v=476mA4zprB" color="#e11422">
    <link rel="shortcut icon" href="/favicon.ico?v=476mA4zprB">
    <!-- Tab Color iOS Safari -->
    <meta name="apple-mobile-web-app-title" content="#e11422">
    <meta name="application-name" content="#e11422">
    <!-- Tile Color Microsoft Windows Shortcut -->
    <meta name="msapplication-TileColor" content="#b91d47">
    <!-- Tab Color Android Chrome -->
    <meta name="theme-color" content="#e11422">

Solution

  • In order to use the .png file you must use the type="image/png" attributes, like below.

    <link rel="icon" type="image/png" href="http://example.com/image.png" />
    

    Make sure that the path you have given is the absolute.