Redirect references to root to current alias instead

479 Views Asked by At

I'm using a local install of Apache 2.2.11 and have set up an alias to a folder above the document root (alias localhost/fp/ => /www/dirs/friendpages). I would like to be able to redirect requests to "/favicon.ico" to localhost/fp/favicon.ico as opposed to localhost/favicon.ico with a .htaccess file in localhost/fp. Is this possible and if so, how would I do that?

The reference to favicon.ico, would come from a file such as the following: /www/dirs/friendpages/test.htm (localhost/fp/test.htm) =>

<html>
    <head>
          <link href="/favicon.ico" rel="shortcut icon" type="image/x-icon">
    </head>
    <body>
          Page using favicon.ico
    </body>
</html>

I have made several attempts using RewriteRule. For example,

RewriteRule ^/favicon.ico$ /www/dirs/friendpages/favicon.ico

...but have not been able to find a solution. It appears that I'm confused about what exactly Pattern is and what Substitution should be (given RewriteRule Pattern Substitution). I'm especially confused about how I would set up a RewriteRule distinguishing a relative file reference such as the one via the link tag above vs. an absolute one like "localhost/fp/favicon.ico" (the latter obviously not requiring a redirection).

I also enabled rewrite logging using the following in my httpd.conf file:

RewriteLogLevel 9
RewriteLog /logs/rewrite.log

...but all I get, when loading the above listed localhost/fp/test.htm with the above stated RewriteRule is this:

127.0.0.1 - - [04/Nov/2011:05:09:51 --0700] [localhost/sid#14f44f8][rid#2619268/initial] (3) [perdir /www/dirs/friendpages/] strip per-dir prefix: /www/dirs/friendpages/test.htm -> test.htm
127.0.0.1 - - [04/Nov/2011:05:09:51 --0700] [localhost/sid#14f44f8][rid#2619268/initial] (3) [perdir /www/dirs/friendpages/] applying pattern '^/favicon.ico$' to uri 'test.htm'
127.0.0.1 - - [04/Nov/2011:05:09:51 --0700] [localhost/sid#14f44f8][rid#2619268/initial] (1) [perdir /www/dirs/friendpages/] pass through /www/dirs/friendpages/test.htm

I have discovered that an alternative way (short of hardcoding the appropriate reference itself) to achieve the desired result is to set up a virtual host (as opposed to simply using an alias) but my gut-feeling is that this should also be possible using mod_rewrite and .htaccess... just how?

1

There are 1 best solutions below

2
On

Your RewriteRule looks fine to me, I'm not sure why it's not working. (You do have RewriteEngine on?)

Since /fp is already aliased to /www/dirs/friendpages, maybe you should just adjust your link href instead:

  1. Change it to a relative one:

    <link href="favicon.ico" rel="shortcut icon" type="image/x-icon">
    

    That will implicitly refer to /fp/favicon.ico.

  2. Change it to point explicitly to /fp/favicon.ico:

    <link href="/fp/favicon.ico" rel="shortcut icon" type="image/x-icon">
    

Either way, the href will point to /fp/favicon.ico, which will resolve to /www/dirs/friendpages/favicon.ico. If the file is there, then you won't need a rewrite rule for it any more.

I agree that a virtual host is way overkill for this problem. A RewriteRule or correct href should be all you need to get your favicon.