Using RewriteCond to exclude directories

Topics: Developer Forum, User Forum
Jul 19, 2007 at 8:49 PM
I am having an issue using RewriteCond to exclude a directory.

For example I want to exclude a RewriteRule from the directory /something so that it does not have any rules applied to it. I also want to have all the other index.html or home.html pages to go to the root of that directory. So that it should look like:

/index.html -> /
/moo/index.html -> /moo
/something/index.html -> no change

The rule I have tried is as follows:

RewriteCond %{REQUEST_URI} ^!/something/
RewriteRule ^(.*)/(index|home)\.html$ $1/ [R=301]

From other examples on the web this should work but I am at a loss as to why it isn't. Any help is appreciated.
Jul 25, 2007 at 12:54 PM
Edited Jul 25, 2007 at 12:55 PM
I have a similar requirement and have been completely unsuccessful. I'm by no means a Perl RegEx pro, but after three days of research I'm about to give up. In my case I have a rule in place restricting remote linking to images, but I need to exclude a series of images tied to an email newsletter we send with linked images.

I've tried the following:

RewriteCond %{HTTP_URL} ^(?!/images/email)
RewriteCond %{HTTP_URL} ^!/images/email

as well as appending a (.*) after each of those. I've gone through numerous possibilities, and I either don't get the exception or it ends up allowing all images. I'm at my wit's end! Can anyone help?

Jul 27, 2007 at 8:15 PM
Sueto try this:-
RewriteRule ^(?:!(?:/something/.*$))|(.*)/(?:index|home)\.html$ $1 [I,R=301]
but remember /moo/me.html will go through.....

Joshcdsi - you should really submit the image blocking rule which would include referrer - but did you try $ after the rule? like:-
RewriteRule ^(?!/images/email/.*$ /forbidden.jpg [I,R])

Jul 27, 2007 at 9:57 PM
I probably should have been more specific in my needs. What I'm looking to do is block image linking to all images except any that begin with "email". So for example, here's what I'd like to see:

/images/productimage.jpg <-- blocked
/images/email_header.jpg <-- NOT blocked

I also need to be able to include my RewriteConds to allow certain domains. This is why I figured it would be a RewriteCond that I'd have to use, instead of embedding it into the RewriteRule.

Jul 30, 2007 at 9:01 PM

I took most of this from readme example 10 except from the last 2 lines........have you tried this?

Example 10:
 1   RewriteCond %{HTTP_REFERER} ^(?!HTTP_REFERER) 
 2   RewriteCond %{HTTP_REFERER} ^(?!https?://(?:www\.)mysite\.net/)   [I]
 3   RewriteCond %{HTTP_REFERER} ^(?!https?://203\.0?30\.0?46\.178/)
 4   RewriteCond %{HTTP_REFERER} ^(?!https?://(?:www\.)foo\.com/)      [I]
 5   RewriteCond %{HTTP_REFERER} ^(?!https?://203\.0?30\.0?46\.181/)
 6   RewriteRule ^/(?!(?:images/email)).*\.(?:jpg|gif)$ /forbidden.jpg   [I,L]
 7   RewriteRule ^forbidden\.jpg$ /forbidden.jpg    [I,L]

I would recommend omitting line 7 and placing the forbidden.jpg in /images/email/ folder.....then replace /forbidden.jpg with /images/email/forbidden.jpg in line 6 - but thats up to you.