Search code examples
phpapache.htaccessmod-rewriteinternal-server-error

Clashing rules in .htacess throwing an internal server error


I'm trying to remap old images paths to new ones, on a site that's built with Codeigniter.

Old paths look like this:

mydomain.com/images/maps/MI/206717178X.jpg

and new paths look like this:

mydomain.com/resources/images/products/front/MI/206717178X.jpg

This is my .htaccess file:

 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^images/maps/([A-Z]{2,3})/(.*)$ resources/images/products/front/$1/$2 [L]
 RewriteRule .* index.php/$0 [PT,L]

And I'm getting Internal Server Error: The server encountered an internal error or misconfiguration and was unable to complete your request.

If I remove the last line, the remapping works well, but of course, the whole website doesn't work, because it needs the index.php in all the urls...

So there seems to be a clashing between the last line and the remapping line, but I don't know what it is. Any pointers would be highly appreciated!


Solution

  • RewriteCond only applies to very next RewriteRule. Try this code:

    RewriteEngine on
    
    # skip files/directories from all the rules below
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    RewriteRule ^images/maps/([A-Z]{2,3})/(.*)$ resources/images/products/front/$1/$2 [L]
    RewriteRule ^ index.php/$0 [L]