views:

347

answers:

6

Duplicate:

How to rewrite non existant files to ‘default’ files? (.htaccess)

How would I "rewrite" to a location if a file doesn't exist? I don't want to use a 404 redirect, but an actual rewrite.

So for example, let's say it is a directory with images. If the image isn't found, then it rewrites to a default image?

I.e.,

images/1.jpg 
images/2.jpg 
images/default.jpg

if someone tried to access "website.com/images/3.jpg", since that doesn't exist, I want it to go to: "website.com/images/default.jpg"

This was a previous "posted" solution, but didn't quite work:

RewriteCond %{REQUEST_FILENAME} !-f [NC] 
RewriteRule /images/.* /images/error.jpg [L]

It still doesn't "get" the right image (just goes as a regular 404 request).

+2  A: 
RewriteCond %{REQUEST_FILENAME} !-f [NC] 
RewriteRule ^images/.* /images/error.jpg [L]

Obviously this only redirects if missing file is under /images/... but you can easily modify it for your own needs

duckyflip
Don’t forget to mark the begin of the regular expression.
Gumbo
hi ducky flip, thanks -- but it doesn't seem to work?I have say website.com/images/asdf.jpg (obviously doesn't exist),but it is not redirecting it to my "error.jpg"?
me1231 I've just fixed it, you need to use ^images instead of /images
duckyflip
hi ducky, thanks -- I appreciate your help... but it still doesn't work :PIf you don't mind, could you please test it on one of your servers and see if you can get it to work?Thanks!
+1  A: 

You should better send a 404 status code if the file really doesn’t exist rather than just a substitute with a status code other than 404. Otherwise the URL will be handled as valid.

So in your case I recommend you to set the ErrorDocument of the images directory to your default image:

<Directory "/path/to/your/images/">
    ErrorDocument 404 /images/default.jpg
</Directory>

But note that the <Directory> block is only available in the server or virtual host configuration context and not the per-directory context (thus .htaccess).

If you cannot use the above, you could use a custom script as your custom error document to check what URL has been requested (see Request_URI environment variable) and send the default image if necessary. The ErrorDocument directive then should look like this:

ErrorDocument 404 /your-error-404.script
Gumbo
A: 

re-write your 404 document for your images folder:

(In your .htaccess file in your images folder)
ErrorDocument 404 default.jpg

Robert Venables
+2  A: 

Well, your previous posted solution is on the right track, but there's some slight craziness with it. Try this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule images/.* /images/default.jpg [L]
chaos
Don’t forget to mark the begin of your pattern. Otherwise any requested URI path that contains `images/` would be matched.
Gumbo
A: 

HI,

Thanks, here is the exact .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_URI} !-f [NC]
RewriteRule screenshots/.* /screenshots/default.jpg [L]

(That was the "first" time)

I've also tried/revised what you have:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^screenshots/.* /screenshots/header.jpg [L]


I have that located in

/var/www/public/screenshots/.htaccess

Let's say I have "1.jpg,2.jpg,3.jpg,default.jpg" in that directory. If I go to any of those images, it works fine. If I go to

if I go to www . mywebsite . com/screenshots/nada.jpg, then it gives me an error message and says: "Not Found: The requested URL /screenshots/nada.jpg was not found on this server."

You are using REQUEST_URI and not REQUEST_FILENAME as I've stated in my solution.
duckyflip
and please, don't add new Answers each time, you can just modify your original post
duckyflip
hi -- I've revised my post (I forgot I was trying a different solution). I've tried what you had in your solution, and 're-edited' the .htaccess sample.
A: 

hi -- I've revised my post (I forgot I was trying a different solution). I've tried what you had in your solution, and 're-edited' the .htaccess sample.

I did try what you mentioned, and that didn't work. Specifically: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f [NC] RewriteRule ^screenshots/.* /screenshots/header.jpg [L]

Any other ideas?

Thanks, I appreciate your time.