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).

0


a source to share


4 answers


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

      



Obviously this is only a redirect if the missing file is under / images / ... but you can easily modify it to suit your needs

+2


a source


Ok, your previous posted solution is on the right track, but there is a bit of laughter with it. Try the following:



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

      

+2


a source


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

      

+1


a source


rewrite 404 file for images folder:

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

0


a source







All Articles