3

My software supports multiple domains pointed at the exact same directory, I use shared hosting. I need to have each domain's favicon load from directories with their respective host names. Here is a visual...

http://www.example1.com/favicon.ico
public_html/www.example1.com/favicon.ico

\

http://www.example2.com/favicon.ico
public_html/www.example2.com/favicon.ico

\

http://www.example3.com/favicon.ico 
public_html/www.example3.com/favicon.ico

I've tried some rewrites along the lines like this without any success...

RewriteEngine on
RewriteRule ^favicon\.ico$ %{HTTP_HOST}/favicon\.ico

Things to keep in mind...

1.) I use shared hosting so remember that the answer I need should be short and simple.

2.) I will only accept a DYNAMIC answer, I will only use the %{HTTP_HOST} variable and NOT a static domain name as I will not be manually editing my .htaccess file every single time I add a domain name.

3.) I may end up putting a .htaccess file in those sub-directories though I do not at the moment, an exception for the favicon would be greatly appreciated though is not necessary for me to accept the answer.

4.) I'll be more than happy to make any clarifications.

John
  • 1
  • 13
  • 98
  • 177

5 Answers5

3

Use this code:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On

RewriteRule ^(favicon\.ico)$ %{HTTP_HOST}/$1 [L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • The first line seems to be more of your flavor of approaching things, hasn't had any effect for the answers. Also your version of the rule looks good and I've got cache disabled in my browser and it's still not working for some reason. I've checked my Apache error log and I'm not seeing anything, no .htaccess files in the sub-directories either... – John Feb 12 '12 at 19:50
  • One minute, determined something that was conflicting AFTER this (put the rule before everything else except of course turning modrewrite on)... – John Feb 12 '12 at 20:24
  • Turns out the main rewrite rule for everything else was overriding this rule (like how CSS rules override previous rules). I added an exception. The rule I posted originally actually DOES work. The setup is highly sensitive and my local/live servers don't agree with the Apache syntax half the time in part because of different paths though also because of different configurations. So this is helping to increase my understanding of how one line can effect another so up-voted you in addition since you're trying to help me out and the problem was on my end, thanks for your patience. – John Feb 12 '12 at 20:32
  • You're welcome John. Glad that you dug deep and resolved the root cause. – anubhava Feb 13 '12 at 06:09
0

I have been struggling with this issue too but I finally fixed it using the following rule:

<IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule ^/favicon\.ico$ /sites/all/themes/mytheme/favicon.ico
</IfModule>

I stuffed this into a virtual host declaration. You can do this for each of your virtual hosts, all you need to do is point the second part to the correct icon! This solves all of my favicon problems, even for Firefox :)

(Tested on FF25, Safari 6.1, IE8 and IE10)

Michel
  • 1,395
  • 13
  • 14
0

This one worked better in my case

RewriteCond $0 !=images/favicon.ico
RewriteRule ^([^/]+/)*favicon\.ico$ /images/favicon.ico [L,NC]
Alex
  • 631
  • 1
  • 8
  • 33
0

To support all possible browsers and platforms, in addition to favicon.ico file, need to have files such as android-chrome-192x192.png, apple-touch-icon.png, favicon-32x32.png, etc...

Here is rewrite rule to support them all:

RewriteRule ^(favicon.*\.(ico|png)|apple-touch-icon.*\.png|android-chrome.*\.png|mstile.*\.png|safari-pinned-tab.*\.svg)$ /favicons/%{HTTP_HOST}/$1 [L,NC]

This will serve favicons including Apple Touch, Android Chrome, Windows and other favicons from /favicons/<DOMAIN_NAME> folder.

Dima L.
  • 3,443
  • 33
  • 30
0

I had a problem with favicon in files on subdomain

I was struggling with redirect for favicon in htaccess for only one subdomain for a long time.

My case was that all domain take favicon from public/ directory. One subdomain (let's call it 'subdomain_a') is configured to take it from another directory and it works.

Problem appeared when a file was opened on subdomain_a. The favicon in file view (f.e. pdf-viewer) was taken from public/ directory, not from configuration of subdomain_a.

Here is my solution:

# Redirect for favicon
RewriteCond %{HTTP_HOST} ^www.subdomain_a.com
RewriteCond %{REQUEST_URI} ^/favicon.ico$
RewriteRule ^(.*)$ /path/to/favicon/for/new/domain/$1 [R=301,L]
ekimas
  • 393
  • 5
  • 10