1

I'm attempting to avoid urls like this one:

images/

or

images/valid-filename.png

or

images/invalid-filename.png

from being rewritten

and I've checked other example questions like .htaccess mod_rewrite on root directory but need it to skip all other directories and .htaccess mod_rewrite exemptions and this one https://stackoverflow.com/questions/7209746/can-mod-rewrite-skip-a-folder but I've not managed to get the rules to achieve what I'm after.

Here is what I have right now:

# Enable URL Rewriting
Options -Indexes
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^(scripts|styles|images)(/.*|$) - [L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/robots.txt
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # This solved the problem
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  RewriteCond %{REQUEST_URI} !images/.* [NC]
  RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>

If I navigate to www.site.com/images/, I get redirected to www.site.com

If I navigate to www.site.com/images/invalid-filename.png, I get redirected to www.site.com

but www.site.com/images/valid-filename.png loads correctly.

Could someone help to explain how to achieve the rewrite behaviour described above?

Many thanks

Community
  • 1
  • 1
MyStream
  • 2,533
  • 1
  • 16
  • 33

1 Answers1

2

Try adding this RewriteCond: RewriteCond %{REQUEST_URI} !images/.* [NC]
It will check whether the URI begins with images/ and if it does, the rule will not be met.

Christopher
  • 2,005
  • 3
  • 24
  • 50
  • Hi Christopher Brix. That was perfect. This condition belongs after the other RewriteCond rules. I've updated my question to reflect your answer. Thank you very much! – MyStream Jun 23 '12 at 11:13
  • Actually - spoke to soon - a php script returned 'exit' on the url so this didn't actually work in the end - but just appeared to. Any possible modifications to try? – MyStream Jun 23 '12 at 11:19
  • @MyStream That's strange. It's working for me. AND: Your first RewriteRule `RewriteRule ^(scripts|styles|images)(/.*|$) - [L]` avoids every url starting with `images` from being rewritten, too. You could add `R=302` to your second RewriteRule. This would force a redirect so you could see whether the rule is met or not. Maybe there is a totally different error? – Christopher Jun 23 '12 at 11:34