How to set favicon default for all pages using htaccess
Solution 1
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.
Solution 2
Without testing, something along these lines:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond favicon.ico
RewriteRule .* path/to/shared/favicon.ico [L]
Solution 3
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]
Solution 4
RewriteEngine on
RewriteBase /
RewriteRule "^(.+)favicon\.ico(|\?.+)$" "/favicon.ico" [PT]
Solution 5
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.
Related videos on Youtube
Arshpreet Wadehra
Full Stack Developer (MERN) | Open Source Contributor | JS Geek | Web Enthusiast Twitter LinkedIn
Updated on September 20, 2020Comments
-
Arshpreet Wadehra about 2 yearsI want to Set Favicon for All files in my site using htaccess ??
-
baig772 over 10 yearsDon'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
-
-
AlienWebguy over 10 yearsThe 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. -
Vidde about 7 yearsWhen you say almost... in what way does it not work?
-
Ярослав Рахматуллин about 7 yearsI was trying to apply that rule in a sub-folder (not /). -
Vidde almost 7 yearsWhere do you put .htaccess file in relation to that sub-folder?
-
Ярослав Рахматуллин almost 7 yearsThe relation to the subfolder from the .htaccess file is ., meaning it is in the same folder. I'm not going to chat about this again. Thank you for trying to help. -
Giulio Caccin about 3 yearsHi! you can edit your own answer with the comment you just wrote here. -
MarsAndBack about 3 yearsKeep 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).