SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast Illicious's Avatar
    Join Date
    Feb 2002
    Location
    NYC
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Broken Image Tracking Htaccess

    Didnt know where to post this but ive been looking for a way to track very unoticable images so i can do the fixing where its needed.

    Thing is i design many templates and alot of times the finished template looks good but i get a error report email and that means theres a image missing somewhere.

    Is there a way with php or better yet htaccess to replace a broken image with another?

    This way i could use a very loud flashing gif image to show me where the image is missing.

    Thanks.
    "History is indeed little more than the register of the crimes, follies, and misfortunes of mankind."
    Edward Gibbon British Historian 1737-1794

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Illicious
    Didnt know where to post this but ive been looking for a way to track very unoticable images so i can do the fixing where its needed.

    Thing is i design many templates and alot of times the finished template looks good but i get a error report email and that means theres a image missing somewhere.

    Is there a way with php or better yet htaccess to replace a broken image with another?

    This way i could use a very loud flashing gif image to show me where the image is missing.

    Thanks.
    Hi,
    most probably there are other methods but I thought this:
    ( mod_rewrite must be installed, obviously )

    /.htaccess
    Code:
    # Normal 404 goes here
    ErrorDocument 404 /404.php
    
    RewriteEngine On
    
    # Is the request an image ?
    RewriteCond %{REQUEST_URI} \.(gif|jpe?g)$ [NC]
    
    # If it's an image, does it exist ?
    RewriteCond %{REQUEST_FILENAME} !-f
    
    # No it does not exist so then redirect internally to your funny image
    RewriteRule ^.*$ /funny.gif [L]
    it's self commented.

    If you are interested on the overhead that this method could give,
    I post here the log file of the rewrite engine:

    # Request /47.jpg (it exists)
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/47.jpg -> 47.jpg
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^.*$' to uri '47.jpg'
    RewriteCond: input='/47.jpg' pattern='\.(gif|jpe?g)$' => matched
    RewriteCond: input='c:/usr/www/mytest.net/htdocs/47.jpg' pattern='!-f' => not-matched
    [per-dir c:/usr/www/mytest.net/htdocs/] pass through c:/usr/www/mytest.net/htdocs/47.jpg


    # Request /48.jpg (it does not exist)
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/48.jpg -> 48.jpg
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^.*$' to uri '48.jpg'
    RewriteCond: input='/48.jpg' pattern='\.(gif|jpe?g)$' => matched
    RewriteCond: input='c:/usr/www/mytest.net/htdocs/48.jpg' pattern='!-f' => matched
    [per-dir c:/usr/www/mytest.net/htdocs/] rewrite 48.jpg -> /funny.gif
    [per-dir c:/usr/www/mytest.net/htdocs/] internal redirect with /funny.gif [INTERNAL REDIRECT]
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/funny.gif -> funny.gif
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^.*$' to uri 'funny.gif'
    RewriteCond: input='/funny.gif' pattern='\.(gif|jpe?g)$' => matched
    RewriteCond: input='c:/usr/www/mytest.net/htdocs/funny.gif' pattern='!-f' => not-matched
    [per-dir c:/usr/www/mytest.net/htdocs/] pass through c:/usr/www/mytest.net/htdocs/funny.gif


    # Request /andrea.html (it does not exist)
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/andrea.html -> andrea.html
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^.*$' to uri 'andrea.html'
    RewriteCond: input='/andrea.html' pattern='\.(gif|jpe?g)$' => not-matched
    [per-dir c:/usr/www/mytest.net/htdocs/] pass through c:/usr/www/mytest.net/htdocs/andrea.html
    [per-dir c:/usr/www/mytest.net/htdocs/] strip per-dir prefix: c:/usr/www/mytest.net/htdocs/404.php -> 404.php
    [per-dir c:/usr/www/mytest.net/htdocs/] applying pattern '^.*$' to uri '404.php'
    RewriteCond: input='/404.php' pattern='\.(gif|jpe?g)$' => not-matched
    [per-dir c:/usr/www/mytest.net/htdocs/] pass through c:/usr/www/mytest.net/htdocs/404.php

    Pretty nice, ya ?

    Hope It Helps,


    Thank you,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  3. #3
    SitePoint Enthusiast Illicious's Avatar
    Join Date
    Feb 2002
    Location
    NYC
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I tried that and it returns a directory doesnt exists.
    I guess i dont have mod_rewrite installed.

    I have this 404.php script that i use to report when somethin is broken but it doesnt send me what the excact file is.
    Maybe you can help:
    Code:
     <?
    # 404.php - Last updated 3/1/2002, initially released 8/10/2000
    # Trap 404 errors and mail a notice to the webmaster
    # Set these variables to configure the script:
    # Set $domain to your domain name (no www)
    $domain = "shaolin-venoms.com";
    # Set $docroot to the URL of the directory which contains your
    # .htaccess file. Don't include trailing slash.
    $docroot = "http://www.shaolin-venoms.com";
    # Font face you'd like to use on the 404 page
    $fontface = "Verdana";
    # Font size you'd like to use on the 404 page
    $fontsize = "2";
    # Background color of the 404 page (default is white)
    $bgcolor = "#000055";
    # Text color you'd like to use on the 404 page (default is black)
    $textcolor = "#ffffff";
    # This script is capable of mailing the details of each 404 error
    # to the webmaster. Use the $reportlevel variable to control when
    # you receive these reports.
    #
    # 0 = don't use the email capabilities
    # 1 = send email only if the error's referer contains your domain name
    # (i.e. the 404 was generated by a broken link on your site)
    # 2 = send email any time a 404 error is generated (useful for tracking
    # broken links at other sites which link to you)
    $reportlevel = 2;
    # Set $emailaddress to the email address of whoever should be
    # notified of 404 errors. Don't escape the @ symbol.
    # You can leave this unassigned if you're not using email features.
    $emailaddress = "shaolin-venoms@illicious.net";
    $ref = $_SERVER['HTTP_REFERER'];
    $url = parse_url($ref);
    
    #
    #Configuration ends here!
    #
    #Until I rewrite my scripts, this will suffice for bg-compatibility;
    if(phpversion() >= "4.2.2" ){
    extract($_POST);
    extract($_SERVER);
    extract($_ENV);
    }
    # The print_details function is what prints the 404 error to
    # the visitor. 
    function print_details(){
    # Request access to the global variables we need
    global $fontface, $fontsize, $docroot, $REQUEST_URI, $reportlevel;
    global $bgcolor, $textcolor;
    # Print the 404 error in web format
    echo <<<EOT
    <html><head><title>404 Not Found</title></head>
    <body bgcolor="$bgcolor" text="$textcolor">
    <b><h1>404 Not Found</h1></b>
    <p><font face="$fontface" size="$fontsize">
    We're sorry. The page you requested, $docroot$REQUEST_URI, doesn't exist
    on this server.</font></p>
    <p>Return to <a href="<A href='http://www.shaolin-venoms.com/"><strong>Shaolin'>http://www.shaolin-venoms.com/"><strong>Shaolin Venoms</strong></a></p> 
    EOT;
    # If an email report is being generated, let the visitor know:
    if ($reportlevel != 0){
    echo "<p><font face=\"$fontface\" size=\"$fontsize\">";
    echo "The details of this error have automatically been mailed to the webmaster."; 
    }
    return;
    }
    
    # The send_email function sends the details of the 404 error to the
    # webmaster. 
    function send_email(){
    # Request access to the global variables we need
    global $REQUEST_URI, $REQUEST_FILENAME, $HTTP_REFERER, $emailaddress, $REMOTE_ADDR, $docroot;
    # Build the $errortime variable to contain the date/time of the error.
    $errortime = (date("d M Y h:m:s" )); 
    # Create the body of the email message
    $message .= "404 Error Report\n\nA 404 error was encountered by $REMOTE_ADDR";
    $message .= " on $errortime.\n\n";
    $message .= "The URI which generated the error is: \n$docroot$REQUEST_URI\n\n";
    $message .= "The referring page was: $ref $REQUEST_FILENAME $filename $script_url $HTTP_REFERER $refer $url";
    $message .= "The referring page was: $REQUEST_FILENAME";
    ;
    # Send the mail message. This assumes mail() will work on your system!
    $headers = "From: $emailaddress\nDate: $errortime -0600\n";
    $subject = "404 Error: $docroot$REQUEST_URI";
    mail($emailaddress, $subject, $message, $headers);
    
    return;
    }
    # Done with function declarations. Main function begins here.
    header("404 Not Found" );
    # Send a 404 error to the user's browser
    print_details();
    # See whether or not we should send an email report. If so, do it.
    if ($reportlevel != 0)
    if ($reportlevel == 1) {
    if (eregi($domain,$HTTP_REFERER)) 
    send_email(); }
    else
    send_email();
    
    # Close up the HTML tags
    echo "</body></html>";
    ?>
    "History is indeed little more than the register of the crimes, follies, and misfortunes of mankind."
    Edward Gibbon British Historian 1737-1794

  4. #4
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> I tried that and it returns a directory doesnt exists.
    A 404 or a forbidden ?

    >> I guess i dont have mod_rewrite installed.
    Uhmm, if you don't have mod-rewrite installed you should have a 500.
    So could you post what you had into your error_log file ( if you have access to it ) ?


    Thank you,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  5. #5
    SitePoint Enthusiast Illicious's Avatar
    Join Date
    Feb 2002
    Location
    NYC
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I aint that advanced in server cus i dont know what log file to look at. Theres many in my server.

    One error_log file has some of this in it:
    Code:
    [Sun Apr 20 02:17:42 2003] [error] [client ***.**.***.***] File does not exist: /var/www/html/default.ida
    [Sun Apr 20 02:32:14 2003] [notice] child pid 1433 exit signal Segmentation fault (11)
    In my site's root directory i have a htaccess with this part:
    "History is indeed little more than the register of the crimes, follies, and misfortunes of mankind."
    Edward Gibbon British Historian 1737-1794

  6. #6
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> it returns a directory doesnt exists.
    Did you remember what it said exactly ?

    Also try to add before RewriteEngine On this:
    Options +FollowSymLinks



    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  7. #7
    SitePoint Enthusiast Illicious's Avatar
    Join Date
    Feb 2002
    Location
    NYC
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wonderful! It works and this saves my time by at least 50%!
    Made a test broken image page and it gets replaced by the no picture gif image perfectly.
    Outstanding help.
    Thanks.
    "History is indeed little more than the register of the crimes, follies, and misfortunes of mankind."
    Edward Gibbon British Historian 1737-1794

  8. #8
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, this could be very useful!
    Pippo, where do you find the log file for the rewrite engine? And for a hosting situation, would I need to add something to .htaccess to get the rewrite engine to create logs?

    Thanks!

  9. #9
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi :-),

    the log file for rewrite engine can be generated by using the RewriteLog and the RewriteLogLevel directives.
    The problem is that those directives can't be used into an .htaccess file but only within a httpd.conf file.

    I used the log directives only to check what steps were perfomed by the rewrite engine :-).
    It's always for me a new learning experience to see what steps the rewrite engine did.


    Thanks,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •