views:

711

answers:

2

I have a website that employs a generic mod_rewrite rule to push all requests to the index.php page, with the exception of certain file extensions:

RewriteRule !\.(js|ico|gif|jpg|JPG|png|css|php|phtml|pdf|txt|xml)$ index.php

What I need to be able to do is also exclude a certain directory (including any files or sub-directories contained within) from this rule - what is the best solution?

Here is my full .htaccess file, in case something else within it is intefering:

RewriteEngine ON
RewriteCond %{HTTP_HOST} !^www\..*
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{HTTP_HOST} ^([^.]*)\.(co\.uk)
RewriteRule ^.*$     http://www.%1.%2%{REQUEST_URI} [R=permanent,L]

AddHandler application/x-httpd-php .phtml

RewriteRule !\.(js|ico|gif|jpg|JPG|png|css|php|phtml|pdf|txt|xml)$ index.phtml

php_value display_errors "On"
A: 

You could check with RewriteCond %{REQUEST_FILENAME} !-f for any request that doesn't match to an existing filename.

stesch
I'm entirely sure I understand your answer - I tried your suggestion and again it didn't appear to have an effect. I have a directory called blog, that I need to access normally - so visiting mysite.com/blog should display the index.php contained within, but it still gets redirected at the moment.
BrynJ
I meant I'm *not* entirely sure! Thank you for your assistance by the way.
BrynJ
This condition is checked before your actual rewrite. It looks if there is a file that would match. And you want to let all URLs to existent images and styles stay the same, without rewriting.
stesch
+4  A: 

Before the line you have quoted, for a directory named 'style' for instance, you need:

RewriteRule  ^style/  -  [L]

The hyphen means 'no redirection', and the '[L]' means 'last rule', as in don't carry on trying to match the URL to the follwing rules. You can put as many of these lines in as you like, but they must be before the line you give in the question.

jTresidder
Would this work for all files and sub-folders? I tried this solution, but although the redirect is bypassed for my directory, no files are accessible within it?
BrynJ
Yep, I'm running that exact line (as in it was cut and pasted) on my sites right now, and everything in the directories is accessible.
jTresidder
I tried this again, and via Firefox I've established that a Redirect Loop is taking place - it's a WordPress install, and I think that is confusing matters as it includes its own redirect magic. Is there another rule that might be required to get it to play ball?
BrynJ
Just found the solution - the urls defined in WordPress were http://mysite.co.uk/blog - so the infinite loop was bouncing between www and non-www. Changed and all is well - thank you!
BrynJ
No problem, glad you got it sorted :)
jTresidder