Discussion:
Redirect subdirectory favicon requests
(too old to reply)
Chuck Anderson
2009-04-02 20:23:38 UTC
Permalink
I am getting more and more requests for my sites favicon.ico, but in
subdirectories (The user agent is always Google toolbar -
thankyouverymuch).

I searched and found this redirect solution for htaccess:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) http://example.com/favicon.ico [R=301,L]
# The first condition bypasses valid requests for favicon.ico in the root

I put that in my root .htaccess file, but when I tested it with:
http://example.com/subdirectory/favicon.ico

I still got a 404 error.

When I put it in the subdirectory's htacces it worked.

Shouldn't that work out of the root directory?
--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Turn Off, Tune Out, Drop In
*****************************
D. Stussy
2009-04-03 22:26:12 UTC
Permalink
Post by Chuck Anderson
I am getting more and more requests for my sites favicon.ico, but in
subdirectories (The user agent is always Google toolbar -
thankyouverymuch).
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) http://example.com/favicon.ico [R=301,L]
# The first condition bypasses valid requests for favicon.ico in the root
http://example.com/subdirectory/favicon.ico
I still got a 404 error.
When I put it in the subdirectory's htacces it worked.
Shouldn't that work out of the root directory?
Seems like you're doing TOO much work with your rewrite rule. You
shouldn't be trying to rewrite everything. Keep it simple. Try this:

RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]

With this, you should be able to eliminate the rewriteconds.
(Style: I always put the L tag first. It helps with debugging when the
rewrite ruleset gets complicated.)

By placing a "(.+)" in front of the literal "/favicon.ico"(EOS) string, it
requires that there be something else and therefore won't match the root
directory, but will match subdirectories and poorly written requests that
have "//" (null directories) in them.

If you don't actually have a "favicon.ico" file in your document root, you
will get a 404.
Chuck Anderson
2009-04-06 07:18:25 UTC
Permalink
Post by D. Stussy
Post by Chuck Anderson
I am getting more and more requests for my sites favicon.ico, but in
subdirectories (The user agent is always Google toolbar -
thankyouverymuch).
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) http://example.com/favicon.ico [R=301,L]
# The first condition bypasses valid requests for favicon.ico in the root
http://example.com/subdirectory/favicon.ico
I still got a 404 error.
When I put it in the subdirectory's htacces it worked.
Shouldn't that work out of the root directory?
Seems like you're doing TOO much work with your rewrite rule. You
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]
With this, you should be able to eliminate the rewriteconds.
(Style: I always put the L tag first. It helps with debugging when the
rewrite ruleset gets complicated.)
By placing a "(.+)" in front of the literal "/favicon.ico"(EOS) string, it
requires that there be something else and therefore won't match the root
directory, but will match subdirectories and poorly written requests that
have "//" (null directories) in them.
If you don't actually have a "favicon.ico" file in your document root, you
will get a 404.
Thanks ... that looks like a simpler approach. And I appreciate the
explanation. I still have the same problem though.

I've determined that it has to do with the htaccess file I have in the
subdirectory .... which has only this in it:

# Redirect old image requests
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$ [NC]
RewriteRule (.*) http://example.com/this_subdirectoryy/photos/$1 [R=301,L]

(I moved all my image files from "this_subdirectory" into a subfolder of
that called photos, and put this rewrite into "this_subdirectory" to
handle any old links to them.)

What I I have determined now is that regardless of the form of the
favicon rewrite in the root directory's htaccess ... i.e.,

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) http://example.com/favicon.ico [R=301,L]

or

RewriteEngine on
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]

It does not work .... that is, unless I delete the htaccess file from
the subdirectory. Then the favicon rewrite in the root directory
htaccess file works.

I don't understand why that would be. Doesn't Apache process both files
(subdir first)? If so, what is it about the htaccess file in the
subdirectory that breaks the rewrite in the root?
--
*****************************
Chuck Anderson • Boulder, CO
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
HansH
2009-04-06 15:41:25 UTC
Permalink
Post by Chuck Anderson
I've determined that it has to do with the htaccess file I have in the
# Redirect old image requests
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$ [NC]
Do try
RewriteCond %{REQUEST_FILENAME} \.(jpe?g|gif|png|tiff?)$ [NC]
"a literal dot and either of t wo 'extentions' at the end of the filename,
where 'e' in jpeg is optional"
Post by Chuck Anderson
RewriteRule (.*) http://example.com/this_subdirectoryy/photos/$1 [R=301,L]
What I I have determined now is that regardless of the form of the favicon
rewrite in the root directory's htaccess ... i.e.,
RewriteEngine on
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]
It does not work .... that is, unless I delete the htaccess file from the
subdirectory. Then the favicon rewrite in the root directory htaccess file
works.
For now just add to the .htaccess in your sub:
RewriteBase /
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]


HansH
Chuck Anderson
2009-04-06 20:22:41 UTC
Permalink
Post by HansH
Post by Chuck Anderson
I've determined that it has to do with the htaccess file I have in the
# Redirect old image requests
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$ [NC]
Do try
RewriteCond %{REQUEST_FILENAME} \.(jpe?g|gif|png|tiff?)$ [NC]
"a literal dot and either of t wo 'extentions' at the end of the filename,
where 'e' in jpeg is optional"
Another form ... thanks for the example. The only image files in that
subdir are (and always will be) jpg or gif. So I could simply use
\.(jpg|gif?)$ [NC]
Post by HansH
Post by Chuck Anderson
RewriteRule (.*) http://example.com/this_subdirectoryy/photos/$1 [R=301,L]
What I I have determined now is that regardless of the form of the favicon
rewrite in the root directory's htaccess ... i.e.,
RewriteEngine on
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]
It does not work .... that is, unless I delete the htaccess file from the
subdirectory. Then the favicon rewrite in the root directory htaccess file
works.
RewriteBase /
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]
Actually ... that does not work. I get a 404 when I use that form in the
subdir (with or without RewriteBase).

I have to use this form in the subdir or I get a 404:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) http://example.com/favicon.ico [R=301,L]

But what I do not understand - and am trying to understand - is why the
htaccess file in the subdir breaks the favicon redirect in the root
folder (for that subdir).

I'd rather be able to put the favicon redirect in the root folder and
have it work whether there is an htaccess file in a subdir or not.
--
*****************************
Chuck Anderson • Boulder, CO
http://www.cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
D. Stussy
2009-04-07 06:29:03 UTC
Permalink
Post by Chuck Anderson
Post by HansH
Post by Chuck Anderson
I've determined that it has to do with the htaccess file I have in the
# Redirect old image requests
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$ [NC]
Do try
RewriteCond %{REQUEST_FILENAME} \.(jpe?g|gif|png|tiff?)$ [NC]
"a literal dot and either of t wo 'extentions' at the end of the filename,
where 'e' in jpeg is optional"
Another form ... thanks for the example. The only image files in that
subdir are (and always will be) jpg or gif. So I could simply use
\.(jpg|gif?)$ [NC]
Post by HansH
Post by Chuck Anderson
RewriteRule (.*) http://example.com/this_subdirectoryy/photos/$1 [R=301,L]
What I I have determined now is that regardless of the form of the favicon
rewrite in the root directory's htaccess ... i.e.,
RewriteEngine on
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]
It does not work .... that is, unless I delete the htaccess file from the
subdirectory. Then the favicon rewrite in the root directory htaccess file
works.
RewriteBase /
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]
Actually ... that does not work. I get a 404 when I use that form in the
subdir (with or without RewriteBase).
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) http://example.com/favicon.ico [R=301,L]
But what I do not understand - and am trying to understand - is why the
htaccess file in the subdir breaks the favicon redirect in the root
folder (for that subdir).
I'd rather be able to put the favicon redirect in the root folder and
have it work whether there is an htaccess file in a subdir or not.
Does this work for you when you insert it into the main configuration file?
If so, then the problem is with the unique environment of the htaccess
file.
HansH
2009-04-07 20:53:08 UTC
Permalink
Another form ... thanks for the example. ...
Rewriterules are voodoo on their own, regexs add steroids ;-)
... The only image files in that
subdir are (and always will be) jpg or gif. So I could simply use
\.(jpg|gif?)$ [NC]
No need for the '?' unless some files end .gi'.
The '|' seperates phrazes. The group of phrazes is embraced to
isolated the leading litteral dot -'\.'-
Post by HansH
RewriteBase /
RewriteRule (.+)/favicon\.ico$ /favicon.ico [L,R=301]
Actually ... that does not work. I get a 404 when I use that form in the
subdir (with or without RewriteBase).
I hate rewrites in .htaccess for more than dropping the path upto the / :-(,
and I must have been sleeping ...
Given the R=301 'RewriteBase' is utter nonsense
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} favicon\.ico [NC]
RewriteRule (.*) http://example.com/favicon.ico [R=301,L]
In .htaccess the URL '/some-folder/favicon.ico' is trimmed _before_
so the rule is applied to 'favicon.ico'. Now try
RewriteEngine on
RewriteRule favicon\.ico$ http://example.com/favicon.ico [L,R=301]

Why 'R=301' at all?
Cann't see a reason to tell the ignorant client about a bad URL
So in either or both .htaccess files try just remapping to the correct file:
RewriteEngine on
RewriteBase /
RewriteRule ^(.+/)favicon\.ico$ favicon.ico [L]
But what I do not understand - and am trying to understand - is why the
htaccess file in the subdir breaks the favicon redirect in the root
folder (for that subdir).
This behavior may be relatively new:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteoptions
RewriteOptions inherit
"... In per-directory context this means that conditions and rules of the
parent directory's .htaccess configuration are inherited."


HansH

Loading...