# Can i force a favicon.ico?

**URL:** <https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127>\
**Category:** Rails\
**Created:** [January 17, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127 "2007-01-17T18:39:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/1 "2007-01-17T18:39:37Z")

</div>

lo there all,

i am running two websites on one server. Both use the same code and both  
domains point to the same public folder. I use different logos by using  
a session variable called session[:domain] that i find with  
env\_table[‘HTTP\_HOST’]

so anyway, both need a different icon to go in the bookmarks and url, i  
know this is done with a favicon.ico, but can i force the server to use  
one for one domain and a different one for the other domain ?

if so, how ?

thanks

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/2 "2007-01-17T18:52:48Z")

</div>

On Jan 17, 2007, at 6:39 PM, Shawn B. wrote:

> use  
> one for one domain and a different one for the other domain ?

If that’s the same Rails application running behind two you could use  
rewrite rules per virtual host:

# first virtual host

RewriteEngine On  
RewriteRule ^favicon.ico$ favicon-vh1.ico [L]

# second virtual host

RewriteEngine On  
RewriteRule ^favicon.ico$ favicon-vh2.ico [L]

That’s written off the top of my head but you see the idea.

– fxn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/3 "2007-01-17T19:04:43Z")

</div>

ok, cool, where do i write those ?

thanks  
shawn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/4 "2007-01-17T19:39:58Z")

</div>

thanks Xavier,  
do i still put the icons in /public/images if both sites are  
automatically routed to /users/index.rhtml?

thanks

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/5 "2007-01-17T19:28:55Z")

</div>

On Jan 17, 2007, at 7:04 PM, Shawn B. wrote:

> ok, cool, where do i write those ?

In the configuration of each vhost.

– fxn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 18, 2007, 1:22am UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/6 "2007-01-18T01:22:40Z")

</div>

ok, i appreciate your help a lot, i am still having trouble with it.  
i put both icons just in the /public directory of the rails app.  
and here is what i have in the virtual host file for one of the sites

\<VirtualHost \*\>  
SetEnv RAILS\_ENV production  
ServerName [www.mysite.com](http://www.mysite.com)  
DocumentRoot /var/www/mysite/public

```
    <Directory /var/www/mysite/public/>
            Options ExecCGI FollowSymLinks
            AddHandler fcgid-script .fcgi
            Order allow,deny
            Allow from all
            RewriteEngine On
RewriteRule ^favicon.ico$ site_a_icon.ico [L]

            RewriteRule ^$ index.html [QSA]
            RewriteRule ^([^.]+)$ $1.html [QSA]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]
            AllowOverride None
    </Directory>
    ServerSignature On

```

# ErrorDocument 500 /500.html

```
    ErrorLog /var/log/apache2/pivotrac/error.log
    CustomLog /var/log/apache2/pivotrac/access.log combined
    LogLevel warn

```

can you see anything that is obvious that i am doing wrong here?  
thanks

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 21, 2007, 1:12am UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/7 "2007-01-21T01:12:52Z")

</div>

On Jan 18, 2007, at 1:22 AM, Shawn B. wrote:

> can you see anything that is obvious that i am doing wrong here?

Sorry for the delay, I’ve been quite busy lately.

The configuration looks OK to me. I have always put my rewrite rules  
outside the Directory section

\<VirtualHost …\>  
\<Directory …\>  
  
RewriteRule …  
…

but if the FCGI dispatcher is being called the rewrite rules are  
working fine.

Did you tell Apache to reload the configuration?

– fxn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [January 17, 2007, 9:38pm UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/8 "2007-01-17T21:38:30Z")

</div>

On Jan 17, 2007, at 7:39 PM, Shawn B. wrote:

> thanks Xavier,  
> do i still put the icons in /public/images if both sites are  
> automatically routed to /users/index.rhtml?

That is not its canonical place, but since we are using rewrite rules  
you can (tweaking the right-side in the examples I sent before). You  
can leave them under public if you want as well.

As you know favicon.ico is normally requested by browsers  
automatically. That is, you don’t link to it, except perhaps by a  
LINK element like this one in the HEAD of your layout:

Now, if I understand the setup correctly you have two vhosts pointing  
to the same Rails application. In particular they share the public  
directory which is the root of the problem.

The browser will still ask for

[http://www.vhost1.com/favicon.ico](http://www.vhost1.com/favicon.ico)

and

[http://www.vhost2.com/favicon.ico](http://www.vhost2.com/favicon.ico)

respectively when they visit any page on those websites.

The point is that you don’t even have a favicon.ico under public. You  
instead have two different .icos somewhere under public and have  
rewrite rules fool the browser. Each vhost has a different rewrite  
rule that rewrites internally the URL to serve its  
corresponding .ico. Perhaps you are not used to this stuff, the  
browser won’t know it is getting something called some other thing in  
the filsystem of the server, nor that the URL was mangled, /  
favicon.ico will be retrieved as if existed.

With this setup both .icos are _available_ using direct URLs since  
the document root is shared, but only the corresponding one will be  
served as /favicon.ico, which is what we want.

– fxn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [April 8, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/can-i-force-a-favicon-ico/82127/9 "2007-04-08T13:23:29Z")

</div>

never got back to you on this,  
but wanted to let you know that your solution worked,  
thanks

sk
