I have this in my .htaccess file:

RewriteEngine on

RewriteRule ^images/chart\.png$    /static/images/chart.png    [L,R=301]
RewriteRule ^images/artwork/(.+).png   /static/artwork/$1.jpg  [L,R=301]

# codeigniter
RewriteCond $1 !^(index\.php|static|images|admin|user_guide|sitemap\.xml\.gz|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

The CodeIgniter part should ignore the images and static folders. However, when I open example.com/images/chart.png I get the CI 404 page. The other 301 redirect is working fine.

Can't fathom this one out, does anyone have any idea?

2 Answers 2


What's happening here is that your rewrite is actually working, but the R=301 is causing a second iteration which causes the 404.

The request comes in for images/chart.png and your rewrite rules go to work. When it matches on your first rule, it causes a 301 redirect. This 301 redirect tells the browser to issue a new request for /static/images/chart.png.

So the new request makes it to your server, and the rewrite rules are executed again. This time, the request doesn't match your custom rules at the top and makes it into the CodeIgniter rule. Since your new URL begins with "static", it matches the CI rule and redirects to /index.php?/static/images/chart.png. Since CI doesn't have a clue how to handle this (I assume), it tosses out the 404 to inform the user it can't find the resource.

What you're going to want to do is to update the CI rule to add the following conditions:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

This will tell mod_rewrite to only execute the rule if the request isn't for a literally existing file (!-f) or directory (!-d).

Once this update is made, the first rule will cause the same redirect for /static/images/chart.png but since that file literally exists at that path, the CI rules will leave it alone, and no new rewriting will be done. The server will then serve up the image normally.

  • Thanks for the response, but that's not right - the current RewriteCond already excludes both the images and static directories. I managed to fix it myself though. Sep 13, 2010 at 11:29
  • 1
    Damn.. my bad. I didn't notice that the RewriteCond was excluding those directories. Sorry about that. Sep 13, 2010 at 12:14

I managed to fix this myself in the end by removing the images directory from the CI RewriteCond clause. (Still not 100% sure why that worked because the request for images/chart.png shouldn't have even got to the CI part.)

  • I'd wager that you were perhaps seeing a cached response. And removing images was simply coincidence. (?)
    – MrWhite
    Aug 10, 2017 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.