favicon and multi site

what about if you are on a multi site install, where would you put the favicon to have it automaticallly recognized by the browser (and no blot the code with link tag)cause there is no real root per se?

Thank you in advance for your help on this

Related posts

Leave a Reply

2 comments

  1. I would use a rewrite to map requests for /favicon.ico to /icons/example.com.ico:

    RewriteCond %{REQUEST_URI} ^/favicon.ico
    RewriteCond %{SERVER_NAME} ^(www.)?([a-z0-9]+)
    RewriteCond %{DOCUMENT_ROOT}/icons/%2.ico -f
    RewriteRule . /icons/%2.ico [L]
    

    Note the regex for line #2 depends on your set-up – is each website its own domain, or a subdomain/subdirectory of a primary domain?

    I would also place a default favicon in the root, which will get pulled if no favicon exists in /icons for the current site.

  2. Another approach is to print the links in the <head>.
    The only caveat is that the following is not working in Sub-directory installs, only in Sub-domain.
    Somehow get_current_blog_id is only returns the ID 1 in when define('SUBDOMAIN_INSTALL', false);.

    <?php
    /**
     * Plugin Name: Multisite Favicons
     * Description: Favicons should be stored in http://example.com/icons/domain_name.ico | If the site is subsite.example.com, the favicon should be subsite.ico
     **/
    
    add_filter( 'wp_head', 'multisite_favicons_wpse_38903' );
    
    function multisite_favicons_wpse_38903()
    { 
        // This function returns true if not Multisite
        if( is_main_site() ) 
            return;
    
        global $current_site;
    
        $details = get_blog_details( get_current_blog_id() );
        $blogname = 
            ( is_subdomain_install() ) 
            ? str_replace( '.' . $current_site->domain, '', $details->domain ) 
            : $details->path;
    
        printf(
            "<link rel='shortcut icon' href='%s/icons/%s.ico' type='image/x-icon'/>n", 
            site_url(),
            $blogname
        );
    }
    

    Interesting blog post: Everything You Ever Wanted to Know about Favicons