Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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 mysite.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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
    
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. –  DisgruntledGoat Sep 13 '10 at 11:29
    
Damn.. my bad. I didn't notice that the RewriteCond was excluding those directories. Sorry about that. –  AgentConundrum Sep 13 '10 at 12:14
add comment
up vote 0 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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