Serving up a default favicon.ico (and robots.txt)

New to mod_rewrite? This is a good place to start.

Serving up a default favicon.ico (and robots.txt)

Postby Guest » Sun Feb 20, 2005 4:43 pm

i'm trying to use the info provided at http://laffey.tv/favicon_error_logs.html to supply a default favicon.ico file for those VirtualHosts that haven't provided one (this is on Apache 2.0.47 on W2K, btw). Without any modification, the error.log file contains:

Code: Select all
[Sun Feb 20 23:47:37 2005] [error] [client 127.0.0.1] File does not exist: C:/Web Sites/test1/favicon.ico


Using a slightly modified version of the code suggested, I've added this to a VirtualHost entry to test:

Code: Select all
<VirtualHost *>
    ServerName test1
    DocumentRoot "C:\Web sites\test1"
    RewriteEngine On
    RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
    RewriteRule  .*favicon\.ico\??.*$         "C:\Web Sites\favicon.ico" [L]
</VirtualHost>


where "C:\Web Sites\favicon.ico" is the location of the default favicon.ico file.When I restart Apache and request a page from site "test1", these lines appear in access.log:

Code: Select all
127.0.0.1 - - [21/Feb/2005:00:12:47 +0000] "GET / HTTP/1.1" 200 11
127.0.0.1 - - [21/Feb/2005:00:12:48 +0000] "GET /favicon.ico HTTP/1.1" 400 298
127.0.0.1 - - [21/Feb/2005:00:12:48 +0000] "GET /favicon.ico HTTP/1.1" 400 298


and the default favicon.ico file isn't shown in the browser address bar. This page http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html suggests 400 is A Bad Thing. And there are two of them for every request... ?

I've played around with variations on the syntax, but can't hit on the right one. Can anyone suggest where I might be going wrong? Thanks!
Guest
 

Postby Caterham » Mon Feb 21, 2005 5:16 am

Code: Select all
\??.*
this is invalid.... you cannot access the queryString in a RewriteRule, because the queryString is striped before processing and is stored in the queryString variable (which can be accessed through a RewriteCindition).

%{DOCUMENT_ROOT}/%{REQUEST_FILENAME}
is usualy invalid, too.
Code: Select all
%{DOCUMENT_ROOT} contains  /var/web/myhost
%{REQUEST_FILENAME} contains  /var/web/myhost/mydir/index.php

%{DOCUMENT_ROOT}/%{REQUEST_FILENAME} would result /var/web/myhost//var/web/myhost/mydir/index.php


The problem here is the local path in the substitution "C:\Web Sites\favicon.ico". Usualy it must be an URL (/favicon.ico), not a local path.
Under some circumstances you can use the local path here, but currently I don't know under which exact circumstances.

But there is a workarround with mod_alias.

Setup an Alias in the main-Serverconfig
Code: Select all
Alias /faviconnotpresent "C:\Web Sites"

Code: Select all
ServerName test1
DocumentRoot "C:\Web sites\test1"

RewriteEngine On
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteRule  ^.*favicon\.ico$         /faviconnotpresent/favicon.ico [PT,L]


ServerName must be a
http://httpd.apache.org/docs-2.0/en/mod/core.html#servername wrote:fully-qualified-domain-name[:port]


Robert
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby Guest » Wed Feb 23, 2005 2:22 am

Brilliant -- thanks for all the info, Robert. Not knowing enough about mod_rewrite, I blindly followed the advice and tried to interpret the Linux-style parts for W2K.

I'll give that a shot and see how it fares. Thanks again.
Guest
 

trying the same thing.... but no working :(

Postby sean69 » Tue Jul 07, 2009 11:00 am

I have been trying exactly the same thing, but the Alias/Rewrite happens even if there is a robots.txt file present...

Here is the vhost directive I have in the individual virtual hosts:
Code: Select all
RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^.*faviconn\.ico$         /favicon404/favicon.ico [PT,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*robots\.txt$ /robots404/robots.txt [PT,L]


and the alias.conf loaded by the apache default config:
Code: Select all
<IfModule mod_alias.c>
Alias   /favicon404     /var/www/vhosts/default/htdocs
Alias   /robots404      /var/www/vhosts/default/htdocs
</IfModule>



I would really like to be able to redirect users to the robots & favicon only if they do NOT exist in the document roots...

any ideas?

help!
-thanks
-sean
Code: Select all
sean69
 
Posts: 3
Joined: Tue Jul 07, 2009 10:47 am

Postby richardk » Tue Jul 07, 2009 1:34 pm

REQUEST_FILENAME and SCRIPT_FILENAME do need the DOCUMENT_ROOT prefixed in <VirtualHost>s.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteRule ^/faviconn\.ico$ /favicon404/favicon.ico [PT,L]

RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteRule ^/robots\.txt$ /robots404/robots.txt [PT,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby sean69 » Tue Jul 07, 2009 1:55 pm

hmmm.... same thing always show the default robots regardless of whether the robots file exists in the vhost directory or not...

I only got it to work like this:

Code: Select all
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^.*robots\.txt$ /robots404/robots.txt [PT,L]


but still if there was a robots.txt file present in the vhost http root then the file in the rewrite rule will still be shown anyway.


But after a little more digging, it appears that mod_rewrite will ALWAYS rewrite when the request condition matches whether the file is there or not.

- is this the case?

-thanks
-sean
sean69
 
Posts: 3
Joined: Tue Jul 07, 2009 10:47 am

Postby richardk » Wed Jul 08, 2009 8:32 am

You are putting the mod_rewrite in the <Virtualhost>, correct?
Have you restarted Apache?

Try changing
Code: Select all
%{DOCUMENT_ROOT}%{REQUEST_FILENAME}

to
Code: Select all
/path/to/virtualhost's/robots.txt

(to test).

If it doesn't work, add a RewriteLog
Code: Select all
RewriteLogLevel 9
RewriteLog "/path/to/rewrite.log"

and see what is logged. (Post the relevant parts here.)

But after a little more digging, it appears that mod_rewrite will ALWAYS rewrite when the request condition matches whether the file is there or not.

- is this the case?

I'm not quite sure i understand, but i'm going to say no as otherwise it would make the condition pointless.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

working... hum.

Postby sean69 » Wed Jul 08, 2009 2:23 pm

ok - I dd get it to work... like this:

Code: Select all
RewriteEngine On
RewriteCond /var/www/vhosts/_domain_name_/httpdocs/robots.txt !-f
RewriteRule ^/robots\.txt$ /DefaultServer/404/robots.txt [PT,L]

RewriteCond /var/www/vhosts/_domain_name_/httpdocs/favicon.ico !-f
RewriteRule ^/favicon\.ico$ /DefaultServer/404/favicon.ico [PT,L]


and it works - but I have like 150 vhosts, I need to place this in a .conf file under /etc/httpd/conf.d/ so that it is read by all vhosts... so the absolute path "/var/www/vhosts/_domain_name_/httpdocs/robots.txt" is not going to fly....
sean69
 
Posts: 3
Joined: Tue Jul 07, 2009 10:47 am

Postby richardk » Wed Jul 08, 2009 2:34 pm

and it works - but I have like 150 vhosts, I need to place this in a .conf file under /etc/httpd/conf.d/ so that it is read by all vhosts... so the absolute path "/var/www/vhosts/_domain_name_/httpdocs/robots.txt" is not going to fly....

That's why i said "(to test)".

I need to place this in a .conf file under /etc/httpd/conf.d/ so that it is read by all vhosts...

That may be a problem. The <VirtualHost>s will almost certainly need
Code: Select all
RewriteOptions Inherit

in them so that the mod_rewrite gets processed. And even then, i'm not sure if the DOCUMENT_ROOT variable will be set correctly.

Try
Code: Select all
RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}$0 !-f
RewriteRule ^/(robots\.txt|favicon\.iso)$ /DefaultServer/404$0 [PT,L]

in the httpd.conf file (or any other "global" file) with
Code: Select all
RewriteOptions Inherit

in the <VirtualHost>s.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 94 guests

cron