How do I ignore a directory in mod_rewrite?

ApacheMod Rewrite

Apache Problem Overview


I'm trying to have the modrewrite rules skip the directory vip. I've tried a number of things as you can see below, but to no avail.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteRule ^vip$ - [PT]
RewriteRule ^vip/.$ - [PT]
#RewriteCond %{REQUEST_URI} !/vip 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

How do I get modrewrite to entirely ignore the /vip/ directory so that all requests pass directly to the folder?

Update:

As points of clarity:

  • It's hosted on Dreamhost
  • The folders are within a wordpress directory
  • the /vip/ folder contains a webdav .htaccess etc (though I dont think this is important

Apache Solutions


Solution 1 - Apache

Try putting this before any other rules.

RewriteRule ^vip - [L,NC] 

It will match any URI beginning vip.

  • The - means do nothing.
  • The L means this should be last rule; ignore everything following.
  • The NC means no-case (so "VIP" is also matched).

Note that it matches anything beginning vip. The expression ^vip$ would match vip but not vip/ or vip/index.html. The $ may have been your downfall. If you really want to do it right, you might want to go with ^vip(/|$) so you don't match vip-page.html

Solution 2 - Apache

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

This says if it's an existing file or a directory don't touch it. You should be able to access site.com/vip and no rewrite rule should take place.

Solution 3 - Apache

The code you are adding, and all answers that are providing Rewrite rules/conditions are useless! The default WordPress code already does everything that you should need it to:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Those lines say "if it's NOT an existing file (-f) or directory (-d), pass it along to WordPress. Adding additional rules, not matter how specific or good they are, is redundant--you should already be covered by the WordPress rules!

So why aren't they working???

The .htaccess in the vip directory is throwing an error. The exact same thing happens if you password protect a directory.

Here is the solution:

ErrorDocument 401 /err.txt
ErrorDocument 403 /err.txt

Insert those lines before the WordPress code, and then create /err.txt. This way, when it comes upon your WebDAV (or password protected directory) and fails, it will go to that file, and get caught by the existing default WordPress condition (RewriteCond %{REQUEST_FILENAME} !-f).

Solution 4 - Apache

In summary, the final solution is:

ErrorDocument 401 /misc/myerror.html
ErrorDocument 403 /misc/myerror.html

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I posted more about the cause of this problem in my specific situation, involving Wordpress and WebDAV on Dreamhost, which I expect many others to be having on [my site][1].

[1]: http://codyaray.com/2010/08/wordpress-webdav-and-dreamhost "Wordpress, WebDAV, and Dreamhost"

Solution 5 - Apache

You mentioned you already have a .htaccess file in the directory you want to ignore - you can use

RewriteEngine off

In that .htaccess to stop use of mod_rewrite (not sure if you're using mod_rewrite in that folder, if you are then that won't help since you can't turn it off).

Solution 6 - Apache

Try replacing this part of your code:

RewriteRule ^vip/.$ - [PT]

...with the following:

RewriteCond %{REQUEST_URI} !(vip) [NC]

That should fix things up.

Solution 7 - Apache

RewriteCond %{REQUEST_URI} !^pilot/ 

is the way to do that.

Solution 8 - Apache

In my case, the answer by brentonstrine (and I see matdumsa also had the same idea) was the right one... I wanted to up-vote their answers, but being new here, I have no "reputation", so I have to write a full answer, in order to emphasize what I think is the real key here.

Several of these answers would successfully stop the WordPress index.php from being used ... but in many cases, the reason for doing this is that there is a real directory with real pages in it that you want to display directly, and the

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

lines already take care of that, so most of those solutions are a distraction in a case like mine.

The key was brentonstrine's insight that the error was a secondary effect, caused by the password-protection inside the directory I was trying to display directly. By putting in the

ErrorDocument 401 /err.txt
ErrorDocument 403 /err.txt

lines and creating error pages (I actually created err401.html and err403.html and made more informative error messages) I stopped the 404 response being generated when it couldn't find any page to display for 401 Authentication Required, and then the folder worked as expected... showing an apache login dialog, then the contents of the folder, or on failure, my error 401 page.

Solution 9 - Apache

I’ve had the same issue using wordpress and found that the issue is linked with not having proper handler for 401 and 403 errors..

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

These conditions are already supposed not to rewrite the url of existing folders but they don’t do their job for password protected folders. In my case, adding the following two lines to my root .htaccess fixed the problem:

ErrorDocument 401 /misc/myerror.html
ErrorDocument 403 /misc/myerror.html

Of course you need to create the /misc/myerror.html,

Solution 10 - Apache

This works ...

RewriteRule ^vip - [L,NC]

But ensure it is the first rule after

RewriteEngine on

i.e.

ErrorDocument 404 /page-not-found.html

RewriteEngine on

RewriteRule ^vip - [L,NC]

AddType application/x-httpd-php .html .htm

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

etc

Solution 11 - Apache

I'm not sure if I understand your objective, but the following might do what you're after?

RewriteRule ^/vip/(.*)$   /$1?%{QUERY_STRING} [L]

This will cause a URL such as http://www.example.com/vip/fred.html to be rewritten without the /vip.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser24557View Question on Stackoverflow
Solution 1 - ApachePatrick McElhaneyView Answer on Stackoverflow
Solution 2 - ApachesteveView Answer on Stackoverflow
Solution 3 - ApachebrentonstrineView Answer on Stackoverflow
Solution 4 - ApacheCody A. RayView Answer on Stackoverflow
Solution 5 - ApacheJayView Answer on Stackoverflow
Solution 6 - ApacheChongFuryView Answer on Stackoverflow
Solution 7 - ApacheGokul MuralidharanView Answer on Stackoverflow
Solution 8 - ApachecarlaronView Answer on Stackoverflow
Solution 9 - ApachematdumsaView Answer on Stackoverflow
Solution 10 - ApachePatView Answer on Stackoverflow
Solution 11 - ApachePeter HoweView Answer on Stackoverflow