Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding images from mod_rewrite rule

Inadvertently my htaccess script is changing image URLs so that any image with "portfolio/" in its URL path is adversely affected.

Is there any way to exclude images from that particular rule?

redirect 301 "/sitemap.xml" http://www.example.com/sitemap.php

RewriteEngine On

RewriteCond %{REQUEST_URI} !portfolio/project
RewriteCond %{REQUEST_URI} /.*portfolio/.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]

RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sitemap.php
RewriteRule ^(.*)$ /index.php/$1 [L]
like image 701
jsuissa Avatar asked Dec 27 '22 17:12

jsuissa


1 Answers

For first part (first rewrite rule) try

RewriteCond %{REQUEST_URI} !portfolio/project
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_URI} /.*portfolio/.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]

I'm not sure why you use RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] in second rewriterule.

RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC] means you don't want to change portfolio to portfolio/project for urls ending with some of allowed image extension. [NC] (case-insensitive) is used to skip JPG, GIF, PnG, etc extensions also

like image 143
Igor Avatar answered Jan 13 '23 12:01

Igor