4

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]
jsuissa
  • 1,754
  • 6
  • 30
  • 64

2 Answers2

7

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

bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
Igor
  • 1,835
  • 17
  • 15
  • Thanks so much. I know the other rewriterule is standard from ExpressionEngine to remove index.php from URLS, but I see what you mean -- don't see a situation where it's relevant. – jsuissa Sep 26 '12 at 17:20
3

If your goal is to exclude images from rewriting rules completely, consider adding the following line as your first rule:

 RewriteRule ^.*\.(gif|jpe?g|png)$ - [L]

It simply stops the rule engine from going further, and does not rewrite the url, if it matches the above pattern.

Johny Skovdal
  • 2,038
  • 1
  • 20
  • 36