Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: 404 image errors

by cLive ;-) (Prior)
on Jun 11, 2002 at 12:31 UTC ( [id://173440]=note: print w/replies, xml ) Need Help??


in reply to 404 image errors

Something like this?
use CGI; my $q = new CGI; my @image_extensions = qw(gif png jpg jpeg); for (@image_extensions) { if ($url =~ /\.${_}$/) { print $q->redirect(-uri=>'http://image.url/here.gif'); exit(0); } }
Amend as you see fit if you want to return same file type - but I think most browsers can interpret the mime without needing the correct extension.

You don't want to return html (the img tag) because that's text.

Please learn CGI - it will make your life easier in the long run - honest!

cLive ;-)

ps - remember, you will need to send the redirect before outputting a text/html header to the browser.

Update: pps - sorry, I should have stated - this was a snippet to go in your existing code (hmmm). I've expanded below...

--
seek(JOB,$$LA,0);

Replies are listed 'Best First'.
Re: Re: 404 image errors
by gav^ (Curate) on Jun 11, 2002 at 12:45 UTC
    That won't work as you forgot to initialise $url to $ENV{'REDIRECT_URL'}.

    I'd also suggest that

    for (@image_extensions) { if ($url =~ /\.${_}$/) {
    was changed to
    for my $ext (@image_extensions) { if ($url =~ /\.$ext$) {
    which makes things a bit more clear

    gav^

      This isn't necessary, but what if .jpg becomes a domain suffix?

      use URI; my $addr = URI->new($url); my $path = $addr->path; for my $ext (@image_extensions) { if ($path =~ /\.$ext$/) { ....

      Probably not worth it to add this in, but I thought I'd point it out anyways :).

Re: Re: 404 image errors
by katgirl (Hermit) on Jun 11, 2002 at 12:37 UTC
    thank you... but.... I still get the default error page :(
      OK, here's a complete (non-tested :) version...
      #!/usr/local/bin/perl -w use strict; use CGI; my $q = new CGI; my @image_extensions = qw(gif png jpg jpeg); my $referer = $ENV{'HTTP_REFERER'}; my $user = $ENV{'REMOTE_ADDR'}; my $usercomp = $ENV{'REMOTE_HOST'}; my $url = $ENV{'REDIRECT_URL'}; my $gmt = gmtime(time); if ($referer) { open(FILE2,">>public/404.txt"); print FILE2 "$url|$referer|$user|$usercomp|$gmt\n"; close(FILE2); } for (@image_extensions) { if ($url =~ /\.${_}$/) { print $q->redirect(-uri=>'http://image.url/here.gif'); exit(0); } } print $q->header, $q->start_html, $q->h1('Whoops!'), $q->h3($url), $q->p('That page does not exist. Please press your back button a +nd try again.'), $q->end_html; exit(0);

      cLive ;-)

      --
      seek(JOB,$$LA,0);

        cLive, I love you and I want to have your babies. *MWAH* :)
        If you made that an internal redirect rather than an external redirect, the browser wouldn't have to pull a second hit from the server... the server would merely substitute the image data.
        ... for (@image_extensions) { if ($url =~ /\.$_\z/) { print $q->redirect("/image/url/here.gif"); exit 0; } } ...
        Now, about them babies... {grin}

        -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://173440]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-20 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found