apache 如何忽略 mod_rewrite 中的目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/163302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:48:46  来源:igfitidea点击:

How do I ignore a directory in mod_rewrite?

apachemod-rewrite

提问by user24557

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.

我试图让 modrewrite 规则跳过目录vip。正如您在下面看到的那样,我已经尝试了很多方法,但都无济于事。

# 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?

如何让 modrewrite 完全忽略/vip/目录,以便所有请求直接传递到文件夹?

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
  • 它托管在 Dreamhost 上
  • 这些文件夹位于 wordpress 目录中
  • /vip/ 文件夹包含一个 webdav .htaccess 等(虽然我不认为这很重要

回答by Patrick McElhaney

Try putting this before any other rules.

尝试将其放在任何其他规则之前。

RewriteRule ^vip - [L,NC] 

It will match any URI beginning vip.

它将匹配任何以 开头的 URI vip

  • The -means do nothing.
  • The Lmeans this should be last rule; ignore everything following.
  • The NCmeans no-case (so "VIP" is also matched).
  • -方法无能为力。
  • L意味着这应该是最后的规则;忽略后面的一切。
  • NC装置没有情况下的(因此“VIP”也匹配的)。

Note that it matches anything beginningvip. The expression ^vip$would match vipbut 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

请注意,它匹配任何开头的内容vip。表达式^vip$将匹配vip但不匹配vip/or vip/index.html。这$可能是你的垮台。如果你真的想把它做对,你可能想和^vip(/|$)这样你不匹配vip-page.html

回答by user24557

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.

这表示如果它是现有文件或目录,请不要触摸它。您应该能够访问 site.com/vip,并且不应发生重写规则。

回答by brentonstrine

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:

您正在添加的代码以及所有提供重写规则/条件的答案都是无用的!默认的 WordPress 代码已经完成了您需要的所有操作:

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!

这些行说“如果它不是现有的文件 ( -f) 或目录 ( -d),请将其传递给 WordPress。添加额外的规则,无论它们有多具体或多好,都是多余的——您应该已经被 WordPress 规则覆盖了!

So why aren't they working???

那他们为什么不工作???

The .htaccessin the vipdirectory is throwing an error. The exact same thing happens if you password protect a directory.

.htaccessvip目录中抛出一个错误。如果您使用密码保护目录,则会发生完全相同的事情。

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

在 WordPress 代码之前插入这些行,然后创建 /err.txt。这样,当它遇到您的 WebDAV(或受密码保护的目录)并失败时,它将转到该文件,并被现有的默认 WordPress 条件 ( RewriteCond %{REQUEST_FILENAME} !-f)捕获。

回答by Cody A. Ray

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.

我在我的特定情况下发布了更多关于此问题的原因,涉及 Dreamhost 上的 Wordpress 和 WebDAV,我希望我的网站上有很多其他人。

回答by Jay

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

您提到您要忽略的目录中已经有一个 .htaccess 文件 - 您可以使用

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

在该 .htaccess 中停止使用 mod_rewrite(不确定您是否在该文件夹中使用 mod_rewrite,如果是,那将无济于事,因为您无法将其关闭)。

回答by ChongFury

Try replacing this part of your code:

尝试替换这部分代码:

RewriteRule ^vip/.$ - [PT]

...with the following:

...具有以下内容:

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

That should fix things up.

那应该解决问题。

回答by Gokul Muralidharan

RewriteCond %{REQUEST_URI} !^pilot/ 

is the way to do that.

是这样做的方法。

回答by Pat

This works ...

这工作...

RewriteRule ^vip - [L,NC]

But ensure it is the first rule after

但确保它是之后的第一条规则

RewriteEngine on

重写引擎开启

i.e.

IE

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

回答by matdumsa

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

我在使用 wordpress 时遇到了同样的问题,发现该问题与没有适当的 401 和 403 错误处理程序有关。

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:

这些条件已经不应该重写现有文件夹的 url,但它们对受密码保护的文件夹不起作用。就我而言,将以下两行添加到我的根 .htaccess 解决了问题:

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

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

当然你需要创建/misc/myerror.html,

回答by carlaron

In my case, the answer by brentonstrine(and I see matdumsaalso 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.

就我而言,brentonstrine的答案(我看到matdumsa也有同样的想法)是正确的...写一个完整的答案,以强调我认为这里真正的关键。

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

其中几个答案将成功阻止 WordPress index.php 被使用......但在许多情况下,这样做的原因是有一个真实的目录,其中包含您想要直接显示的真实页面,并且

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'sinsight 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

关键是brentonstrine 的洞察力,即错误是次要影响,由我试图直接显示的目录内的密码保护引起。通过放入

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.

行并创建错误页面(我实际上创建了 err401.html 和 err403.html 并制作了更多信息性错误消息)当它找不到任何页面来显示需要 401 身份验证时,我停止生成 404 响应,然后文件夹工作正如预期的那样......显示一个 apache 登录对话框,然后是文件夹的内容,或者在失败时,我的错误 401 页面。