Skip Navigation

Ignore Directories in mod_rewrite

 
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]
 

Tags

Syndication

If you like this post, subscribe to my full feed or partial feed.

 

12 comments (Add Yours)

You can keep up to date with this discussion by subscribing to the RSS or Atom feed.
 
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.
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
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.
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
Dario Caregnato
Italy #5: October 4, 2005
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??
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.
 United States #7: March 24, 2006
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.
Could you put the folder names in just one rule?

RewriteRule ^(folder_name|folder2|folder3)/.*$ - [PT]

Or would that introduce performance problems?
Karl
United States #9: May 11, 2006
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.
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
SEO
Australia #11: May 29, 2007
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
 United States #12: April 2, 2008
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]

 

Post Your Comment

 
Only the name and comment fields are required.
 

Live Comment Preview

 United States #13: 1 minute ago

Web Design, Development and Marketing