How can I get junk files displaying correctly in .htaccess?
Duplicate:
How to overwrite nonexistent files to 'Default files'? (.htaccess)
How would I "overwrite" the location if the file doesn't exist? I don't want to use a 404 redirect, but an actual rewrite.
So let's say this is a directory with images. If the image is not found, is it overwritten with the default image?
those.
images/1.jpg
images/2.jpg
images/default.jpg
if someone tried to access "website.com/images/3.jpg" as it doesn't exist I want it to go to: "Website.com/images/default.jpg"
This was the 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 correct image (just goes like a normal 404 request).
You are better off submitting a 404 status code if the file does not really exist, rather than just replacing a status code other than 404. Otherwise, the URL will be treated as valid.
So, in your case, I recommend that you set the ErrorDocument
directory images
to your default image:
<Directory "/path/to/your/images/">
ErrorDocument 404 /images/default.jpg
</Directory>
But note that the block is only available in the server or virtual host configuration context, not the per directory context (thus .htaccess). <Directory>
If you cannot use the above, you can use the custom script as your own error document to check which url was requested (see <environment variable Request_URI
) and send the default image if necessary. The directive ErrorDocument
should look like this:
ErrorDocument 404 /your-error-404.script
a source to share