Mobile app version of vmapp.org
Login or Join
Cofer257

: Apache RewriteRule not redirecting I have this in my .htaccess file: RewriteEngine on RewriteRule ^images/chart.png$ /static/images/chart.png [L,R=301] RewriteRule ^images/artwork/(.+).png

@Cofer257

Posted in: #Htaccess #Redirects

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/.jpg [L,R=301]

# codeigniter
RewriteCond !^(index.php|static|images|admin|user_guide|sitemap.xml.gz|robots.txt)
RewriteRule ^(.*)$ /index.php/ [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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Cofer257

2 Comments

Sorted by latest first Latest Oldest Best

 

@Cofer257

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

10% popularity Vote Up Vote Down


 

@Candy875

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme