Apache .htaccess <Directory not allowed here

Apache .htaccess <Directory not allowed here

我 运行 XAMPP 我正在尝试了解 .htaccess 的工作原理。我有一个看起来像这样的文件结构:

/parent
    /foo
    /bar
    .htaccess

我只想将所有 foo 请求更改为 bar,并在其后附加一个 GET 参数。例如:

foo/
foo/hello.php

变成:

bar/?test=yes
bar/hello.php?test=yes

当我尝试将 <Directory> 指令放入我的 .htaccess 文件时:

<Directory "/foo">
    Options +Indexes
</Directory>

我收到以下错误日志:

[Tue Sep 19 17:23:58.356362 2017] [core:alert] [pid 6336:tid 1900] [client ::1:60018] C:/xampp/htdocs/parent/.htaccess: <Directory not allowed here

我检查了我的 httpd.conf 文件,一切正常。我敢肯定,因为如果我将 .htaccess 文件的内容更改为:

Options -Indexes

它正确显示了 403 错误。如果我将 - 替换为 +,它会显示目录列表。

我做错了什么?

根据 the manual

<Directory...htaccess 中是不允许的。您只能在服务器配置和虚拟主机中拥有它。您应该改用 mod_rewrite

<Directory> 指令在 .htaccess 中是不允许的,为了满足您的要求,您甚至不需要在 .htaccess 中使用它。

您可以在站点根目录(父站点)中使用此规则.htaccess:

RewriteEngine On

RewriteCond %{QUERY_STRING} !(?:^|&)test=yes(?:&|$) [NC]
RewriteRule ^foo/(.*)$ bar/?test=yes [NC,QSA,L]

简短回答:只需删除 .htaccess 文件,您的 error.log 问题就会消失。

更长的答案:

我今天早些时候开始在我的 C:\xampp\apache\logs\error.log 文件中收到相同的消息。该错误突然开始(经过多年顺利使用 localhost),并在我尝试启动 localhost 时与“500 服务器错误”重合。

.htcaccess 文件(今天第一次出现在我的 /Sites 目录中)的内容是:

<Directory>
  AllowOverride All
  Order allow,deny
</Directory>

正如其他人在类似的 .htcaccess 问题中指出的那样,该文件通常不是必需的。

于是我干脆删除了.htcaccess文件,问题立马解决了。 localhost 立即再次开始工作,错误从日志中消失了!