Artur Filipiak
Artur Filipiak

Reputation: 9157

htaccess redirect to file in other directory

How can I load requested file from another directory (using .htaccess) if that file is not found at the requested URL?

For example:

Request:: mysite.com/samples/image_requested.jpg (does not exist)

Output: mysite.com/samples/missing/image_requested.jpg (without showing the exact url in the browser)

I tried this but it gives me a 500 Internal Error:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/samples/
RewriteCond %{REQUEST_URI} !^/samples/missing/
RewriteRule ^samples/(.+)$ /samples/missing/$1

Upvotes: 0

Views: 181

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11799

Here is an example for jpg, png and gif files. It will display always the requested URL (/samples/file.jpg), not the real one (/samples/missing/file.jpg):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule samples/(.*\.(jpg|png|gif)) samples/missing/$1 [L] 

Upvotes: 1

Related Questions