3

I want to Set Favicon for All files in my site using htaccess ??

Arshpreet Wadehra
  • 993
  • 2
  • 9
  • 23
  • where are the current favicons? does each file point to a different one? – Matt Dodge Jun 07 '12 at 05:38
  • 1
    Don't know how to do it from .htaccess, rather you can set it in header part of your template and call the same header on all pages – baig772 Jun 07 '12 at 05:38
  • A more detailed rewrite rule also accounting for non-favicon icons: https://stackoverflow.com/a/37107216/4378314 – Kalnode Nov 10 '19 at 14:30

5 Answers5

9

Rather than specifying a Favicon in htaccess you would be better off using the following META tag within the HEAD area of every page:

<link rel="shortcut icon" href="http://example.com/myicon.ico" />

If this is not possible (perhaps you have a very large static website) you can simply store the file (name it favicon.ico) in your website's root folder (e.g. /public_html/) as browsers will automatically look there first.

Joe Spurling
  • 967
  • 11
  • 22
  • 3
    The benefit of the htaccess method is the OP can keep it out of the web root so not to assign it to any one site, but still deliver it on the initial request. The meta tag referencing a 'shared' domain (likely a cookieless or static asset domain) would force the browser to do a new DNS lookup and open a new http connection, and the common domain would have to support http and https because it wouldn't know which protocol the site fetching it was using. – AlienWebguy Jun 07 '12 at 05:46
  • Keep in mind it's possible some software/apps may just grab the favicon alone, earlier or exclusive of the HTML (ie never grabbing your HTML). – Kalnode Nov 10 '19 at 14:26
8

Without testing, something along these lines:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond favicon.ico 
RewriteRule .* path/to/shared/favicon.ico [L]
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
7

Inspired by this thread: Rewriting path for a specific file using htaccess and using the fact that browsers will look for favicon.ico in root of site:

RewriteEngine On 
Options +FollowSymLinks
RewriteRule ^favicon.ico path_to_favicon_folder/favicon.ico [L]
Community
  • 1
  • 1
Vidde
  • 91
  • 1
  • 2
1
RewriteEngine on
RewriteBase /
RewriteRule "^(.+)favicon\.ico(|\?.+)$"  "/favicon.ico" [PT]
Roland Soós
  • 3,125
  • 4
  • 36
  • 49
1

Add this code

RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond favicon.ico 
RewriteRule .* favicon.ico [L]

If top code is not going to work use this.

RewriteEngine On 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^(.+)favicon\.ico(|\?.+)$"  "/favicon.ico" [PT]

You must put your favicon in public_html directory.

Please DELETE CACHE of your browser or test on a new browser.

I hope it helps.

Shahrad Elahi
  • 774
  • 12
  • 22