2
votes

I need to redirect image files to a diferente folder. I tried using:


    RewriteCond %{HTTP_HOST} ^example.com$ [NC]
    RewriteCond %{REQUEST_URI} (.*)\.(png|gif|jpg|jpeg)
    RewriteRule ^(.*)$ images/$1    [L]

This results in a 500 Internal Server Error, but the following works:


    RewriteCond %{HTTP_HOST} ^example.com$ [NC]
    RewriteCond %{REQUEST_URI} (.*)\.(png|gif|jpg|jpeg)
    RewriteRule ^(media/1.jpg)$ images/$1   [L]

This also works:


    RewriteCond %{HTTP_HOST} ^example.com$ [NC]
    RewriteCond %{REQUEST_URI} (.*)\.(png|gif|jpg|jpeg)
    RewriteRule ^(.*)$ images/media/1.jpg   [L]

I can't figure out why this is happening, any help?

1

1 Answers

2
votes

You're getting 500 error because of looping. Apache keeps running rewritten URIs with mod_rewrite module until they fail to match any rule.

Try this rule with better checks:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !/images/ [NC]
RewriteRule \.(png|gif|jpe?g)$ images%{REQUEST_URI} [L,NC]

This will rewrite image URLs only when /images/ is not present in URI.