Thursday, May 31, 2012

How do I ignore a directory in mod_rewrite?


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


Source: Tips4all

8 comments:

  1. 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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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).

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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.

    ReplyDelete
  7. 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,

    ReplyDelete
  8. This is a really annoying stupid bug, I have been searching the web for ages finally come accross tis post and still nothing works :s

    Might be worth also pointing this here...
    http://www.addedbytes.com/blog/ignore-directories-in-mod-rewrite/

    ReplyDelete