Forum Moderators: phranque

Message Too Old, No Replies

Deliver "Default" image if requested image is not found?

default image for directory

         

factorypower

9:37 pm on Mar 7, 2007 (gmt 0)

10+ Year Member



What I want to do is have a "default" image that is displayed to anyone who requests a image that is no longer in a specific directory. (basically any 404 request in a specific directory)

I have searched online (and dug through a few reference books) for a while without finding what I need. I am fairly sure this can be done with Apache but have been unable put together the pieces I need to make this work...

We are running Apache 1.3.33 on a Mac x serve.

Any help is appreciated...

TIA...

jdMorgan

10:19 pm on Mar 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Apache mod_rewrite [httpd.apache.org] can be used to do this easily. Use a RewriteCond with the -f flag to check for the existence of the requested image, and if it doesn't exist, rewrite the request to the replacement image.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

factorypower

4:06 pm on Mar 8, 2007 (gmt 0)

10+ Year Member



jdMorgan,

Thanks for the push in the right direction... Here is what I have come up with so far:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule $ /default/defaultsm.jpg [L]

Does this look correct and will it work if placed inside the appropriate <Directory> tag?

I think I am on the right path but my new question is there are 3 different size images stored on the site all in the "images/" directory with names "sm" "med" and "lg" (eg: "/images/sm/names-of-images.jpg") I have 3 different size default images that I want to use when a image that no longer exists in that directory is requested. What would be the best way to handle this?

Note that this is a site that is a virtual host so I can easily write the rules inside the Virtual host tags for just that site. But, should I create a new <Directory> tag for each of the 3 directories (and put the rewriterules in each) or just create the 3 rules and somehow specify the directory in each of the rules?

Also, can I just put the "RewriteEngine on" once at the top of the Virtual host tag? Or does it need to be outside the VH tag? Or do I need to put that above every set of rewrite rules?

Once again TIA

,Nathan

jdMorgan

4:35 pm on Mar 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just one rule, shown here rooted in the top-level HTTP-accessible <Directory> or <Location>, will do:

RewriteEngine on
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/images/(sm¦med¦lg)/[^.]+\.(jpg¦gif¦png¦bmp)$ /default/default$1.$2 [L]

You only need one "RewriteEngine on" in the same context/location as your RewriteRules. Note the use of the "!" unary-negative (NOT) operator. You want the rewrite to be invoked only if the requested URL does NOT exist as a file.

The more specific your RewriteRule pattern, the more efficient this will be. Also, this code uses back-references -- So again, please see the info cited/linked above, especially the mod_rewrite docs and the regular-expressions tutorial cited in our forum charter.

You may also wish to consider whether you want to do an internal rewrite, as shown so far, or an external 301-Moved Permanently or 302-Moved Temporarily redirect -- This depends on whether you wish search engines to retain the obsolete image URLs or not (if applicable).

Replace the broken pipe "¦" characters in the pattern above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

factorypower

7:52 pm on Mar 8, 2007 (gmt 0)

10+ Year Member



Taking your example I put this together...

It is not working for me on the mac os x box I am testing it on...

<Directory "/Library/WebServer/Documents/images">
AllowOverride FileInfo
#
Options Indexes FollowSymLinks MultiViews
#
RewriteEngine on
#
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^/(thumb¦reg¦lg)/[^.]+\.(jpg¦gif¦png¦bmp)$ /default/default$1.$2 [L]
#
Order allow,deny
Allow from all
</Directory>

This is exactly what I have in my httpd.conf file except that I have full pipes instead of broken ones...

I corrected my mistake with the directory names (thumb, reg, lg) and also made a change because the directories thumb, reg and lg are all in the images directory along with the "default" directory (which is where the 3 default photos are located...) (so in the images directory is 4 directories thumb, reg, lg and default)

This is just an "out of the box" apache install on a mac os X client box... I looked through the httpd file and it looks like the mod_rewrite is being called (it is not commented out) so I assume it is installed and running...

Maybe my problem lies with the Directory tag I am attempting to utilize?

I think I am really close but if I try to call up a image that is not in one of the 3 image directories I am getting a 404 Not Found error, not the default image...

TIA

Nathan

jdMorgan

8:31 pm on Mar 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> ...and also made a change because the directories thumb, reg and lg are all in the images directory along with the "default" directory...

Understand that mod_rewrite works with local URL-paths not directories, so if thumb, reg, and lg are located in example.com/images/, then the RewriteRule pattern must include that images path as well -- e.g.
RewriteRule ^/images/(thumb¦reg¦lg)/[^.]+\.(jpg¦gif¦png¦bmp)$ /default/default$1.$2 [L]

mod_rewrite code has to be exactly right in order to work -- no wiggle-room whatsoever. So, you'll need to figure out what all the URL-paths are, and incorporate that info into the pattern and the substitution. If you still can't get it working, then please post the full local URL-paths for several examples, and also the server filepaths that correspond to those URL-paths via the DocumentRoot setting. We'll try to help, but we can't work in a vacuum, and the modification and testing work lies primarily with you.

The server error log is often quite helpful, as is the RewriteLog. Take a look and see if there's anything obviously wrong in there.

Jim

factorypower

9:32 pm on Mar 8, 2007 (gmt 0)

10+ Year Member



Here is the full paths (on my test box):

/Library/WebServer/Documents/images/

(on this test box "documents" is the root directory)

Inside the images folder are 4 folders:

default
lg
reg
thumb

Then what I have are 3 images inside the default folder (one for each size image)

So if someone tries to access a image in the "lg" folder that is no longer there I want to use Mod_rewrite to show them the defaultlg.jpg that is in the default folder...

I made the changes as you suggested:

RewriteRule ^/images/(thumb¦reg¦lg)/[^.]+\.(jpg¦gif¦png¦bmp)$ /images/default/default$1.$2 [L]

This still does not seem to be working... (still getting 404)

Do I need to put in the full path from the root of the server?

I modified it and made it like this:

RewriteRule ^/Library/WebServer/Documents/images/(thumb¦reg¦lg)/[^.]+\.(jpg¦gif¦png¦bmp)$ /Library/WebServer/Documents/images/default/default$1.$2 [L]

That did not work either...

I also took it completely out of the surrounding directory tags (allowing it to apply to the whole server... to no avail...

TIA!

,Nathan

jdMorgan

12:01 am on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Posted previously:
mod_rewrite code has to be exactly right in order to work -- no wiggle-room whatsoever. So, you'll need to figure out what all the URL-paths are, and incorporate that info into the pattern and the substitution. If you still can't get it working, then please post the full local URL-paths for several examples, and also the server filepaths that correspond to those URL-paths via the DocumentRoot setting. We'll try to help, but we can't work in a vacuum, and the modification and testing work lies primarily with you.

The server error log is often quite helpful, as is the RewriteLog. Take a look and see if there's anything obviously wrong in there.

(emphasis added)

I hope you will consider that everyone's time for contributing here is limited, and that we can't afford to repeat ourselves...

Jim

factorypower

2:12 am on Mar 9, 2007 (gmt 0)

10+ Year Member



I apologize for wasting your time on this matter... I am VERY green on manual apache manipulation and changes, and truly appreciate the time, information and help that you have provided to me. I will work my way through this via trial and error and if I cannot get it working I will just pack it in and move on... Thanks again for all your help.

,Nathan

factorypower

2:07 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



I got it working this morning (guess the rest did me well) Here it is for anyone else looking to solve a similar problem:

The setup information is as follows:

/Library/WebServer/Documents/images/

with the following folders inside the images folder:

default
lg
reg
thumb

In each of these folders (lg, reg, thumb) are lots of images, but if someone tries to access a image that is no longer in one of the 3 folders (lg, reg, thumb) now it will deliver the correct size image back to them that is stored in the default folder... I created 3 images; defaultlg.jpg, defaultreg.jpg, and defaultthumb.jpg and put them in the default folder. (I only needed to create jpgs since I have controlled folder content and only jpgs are ever put in there, but this solution will work for jpg, gif, png, and bmp but you would have to create default images for every type and name them properly...)

Here is the corrected code:

<Directory "/Library/WebServer/Documents/images">
AllowOverride FileInfo
#
Options -Indexes +FollowSymLinks -MultiViews
#
RewriteEngine on
#
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^(thumb¦reg¦lg)/[^.]+\.(jpg¦gif¦png¦bmp)$ /images/default/default$1.$2 [NC,L]
#
Order allow,deny
Allow from all
</Directory>

Like jd stated earlier be sure to "replace the broken pipe "¦" characters in the pattern above with solid pipes before use; Posting on this forum modifies the pipe characters."

Thanks again jdMorgan for all you help and time!

,Nathan

jdMorgan

3:41 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working! -- It is impossible to determine the vagaries of URL-layout, filesystem-layout, and pre-existing server configuration unless that information is provided in the thread. My frustration shows through occasionally, and combined with my habitually-dry and technical writing style, results in posts that I know I could have worded in a more friendly tone. Please accept my apologies for the "tone" of my post.

[added] It appears that disabling MultiViews, moving the code into /images/.htaccess, and removing the ^images/ URL-path-part from the RewriteRule pattern is what was needed to get this working. Note also that if you have any "jpeg" files, you can include them as well as "jpg" files in the pattern by changing "jpg" to "jpe?g" -- this matches "jpg" or "jpeg" by making the "e" optional. [/added]

Jim

[edited by: jdMorgan at 3:45 pm (utc) on Mar. 9, 2007]