A quick piece of code for you. If you are using mod_rewrite and creating RewriteRules for a website that emulate a directory structure, you might happen across the same problem I've had. If you have actual, real folders on the site as well, you don't want requests for items in those folders to be rewritten. You need a way to prevent the RewriteRule(s) matching the real folders. The easiest way to do this is (I think) by adding a RewriteRule for each of the real folders, like the below. This rule will match any request to those folders and prevent it being rewritten later in the set of rules.
RewriteRule ^folder_name/.*$ - [PT]
Ignore Directories in mod_rewrite, by Dave Child, was posted on 08 September 2005 and has been tagged with apache.
Add a Comment
Thoughts from a Brighton geek about web development, marketing, freelancing, entrepreneurship and fatherhood. Probably not in that order.
26 Comments
I just use the following conditions for rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .......
i.e. not rewritting is request uri resolves to a dir or file.
#1, Analgesia, Netherlands, 8 September 2005. Reply to this.
It's not the easiest way to ignore mod_rewrite, not at all! I found a way I believe it's much easier and above all portable: just use an .htaccess file, put it inside every directory you want to be ignored and inside it write "mod_rewrite off" . That's it ;) the power of inheritance under apache....do you like it? :) let me know at php@caregnato.net
Replies: #23.
#2, Dario Caregnato, Italy, 9 September 2005. Reply to this.
Analgesia: That's fine for a couple of rules. But, for a few hundred, I'm not sure that would be practical. That said, that set of conditions could probably be combined with the rule I posted to make life even easier...
Dario: That's a good solution, but does require many files. Personally, I prefer to keep everything in one file where possible to make things easier to work with later on. Good suggestion though.
#3, Dave Child, United Kingdom, 14 September 2005. Reply to this.
Here's an even easier way, so that you don't have to remember to update it each time you add a folder:
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
Or, if you know how many rules you're going to have (if they're auto-generated or some such), you can use the "skip" flag in the RewriteRule. (WordPress does this with its automatically created rules.)
RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
RewriteCond %{REQUEST_FILENAME} -f [NC]
RewriteRule .* - [S=523]
... a block of 523 rules
Replies: #25.
#4, Isaac Z. Schlueter, United States, 20 September 2005. Reply to this.
Hmmm sorry but I can't understand...the way I suggested uses inheritance, so if you put an .htaccess file with "mod_rewrite off" in the "ignorethisone" directory, also ignorethisone/subfolder1 and ignorethisone/sub1/sub2 etc etc etc won't use anymore mod_rewrite! ;) All you have to do is place an almost empty htaccess file! And, of course, be tidy with you folder structure...isn't this easier than working with regular expressions??
#5, Dario Caregnato, Italy, 4 October 2005. Reply to this.
Dario: The problem is though that you have to do the same thing for every folder you want to ignore. That's a lot of unnecessary htaccess files.
Isaac: Nice rules! Those work perfectly.
#6, Dave Child, United Kingdom, 4 October 2005. Reply to this.
that pass through rewrite rule just saved me from another 30 minutes of trial and error testing.
I was doing something similar with the -d, but I didn't have a -f, that probably would have fixed it for me.
Oh well, this pass through option helped, becuase i think i am only going to have a handful of files that i want to passthrough and not the whole directory.
#7, Fajita, United States, 24 March 2006. Reply to this.
Could you put the folder names in just one rule?
RewriteRule ^(folder_name|folder2|folder3)/.*$ - [PT]
Or would that introduce performance problems?
#8, Mark Kenny, United Kingdom, 23 April 2006. Reply to this.
WordPress gives this to use
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</IfModule>
And I can't access my script in cgi-bin because of it, and anything I have tried from here has not worked, and even made my blog break down in most cases.
#9, Karl, United States, 11 May 2006. Reply to this.
Note that in addition to the RewriteCond solutions listed above, the %{REQUEST_FILENAME} variable is context sensitive.
In a <Directory /doc/root> stanza or a .htaccess file it'll work fine, but in <VirtualHost> %{REQUEST_FILENAME} = %{REQUEST_URI}.
noodl
#10, Vincent Bray, United Kingdom, 23 May 2006. Reply to this.
I'm desperately trying to find a .htaccess for rewriting sub directory to subdomain. For instance,
http://www.domain.com/gardening/roses.html
to
http://roses-gardening.domain.com
Is it possible? I've got wildcard subdomains enabled, i'll check back.
It's for http://www.articlesdb.net
Thanks
#11, SEO, Australia, 29 May 2007. Reply to this.
Thanks!, worked just the way I wanted with a single line except you have to specify DIR name in .htaccess,
but the following works without DIR name with 3 lines.
[code]
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
[/code]
#12, trifid, United States, 2 April 2008. Reply to this.
Thanks trifid, this code works perfectly.
#13, Jon, Canada, 11 June 2008. Reply to this.
Hi,
I'm trying to 'ignore' a zenphoto directory that is in my wordpress installation. Tried your idea, but no success.
I currently have the following in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
which gives me some nice names, but messes up my zenphoto gallery, spitting out raw html.
My root is a bit like this...
wp-content
wp-admin
wp-includes
zenphoto
It's ok for the htaccess to affect the first three, as they're WP, but I need to "ignore" zenphoto. How do I do this?
#14, michael, Unknown, 5 October 2008. Reply to this.
This has just very happily fixed it for me: http://www.andrewrollins.com/2008/01/22/wordpress-and-htaccess-password-protected-directories/
#15, Ed, United Kingdom, 6 October 2008. Reply to this.
I must side with Dario, i'd rather drop a near empty htacess file, than have my hair fall out as I take my entire site down because I mussed up a regex ;)
#16, Justin, Ireland, 10 October 2008. Reply to this.
Thanks Dave Child - I had to use "RewriteEngine Off", but I found your solution to be the best.
I agree - one file consisting of one line, placed in the root of a "free" directory is by far the most elegant solution.
#17, Mike, New Zealand, 24 May 2009. Reply to this.
worked like a charm! :)
#18, Ryan, Cleveland, OH, 8 September 2009. Reply to this.
Really nice to read...
Please post an Easy to understand Tutorial link for my Students
#19, WiserX, Unknown, 16 September 2009. Reply to this.
This saved my a**. I have spent the past 6 hours trying to figure out how to do what you did in 1 line. thank you!!!
#20, Brian, 30 December 2009. Reply to this.
Awesome post! This is the most useful example I could find for this topic and really helped me out finishing up my rule set. Cheers!
#21, g, 9 April 2010. Reply to this.
If you have wordpress then this is the solution.
Replies: #24.
#22, Foo, 23 April 2010. Reply to this.
#2 Thanks, helped me out
#23, James, 28 July 2010. Reply to this.
#22 but how do I implement it?
#24, Chris, 15 August 2010. Reply to this.
#4 that breaks Wordpress CSS for me
#25, Tim, AU, 9 September 2012. Reply to this.
Can break wordpress/Thesis
#26, Tim, 9 September 2012. Reply to this.