Apache 似乎没有检测到我的 .htaccess 文件

Apache doesn't seem to be detecting my .htaccess file

我通过 MAMP 在我的计算机上 运行 一个 Apache 服务器,我似乎无法让它读取我的 .htaccess 文件。我看过的所有解决方案都说要确保我的 AllowOverridehttpd.conf 中设置为 All,确实如此,但这似乎并不能解决我的问题.我知道 .htaccess 没有被读取,因为我在文件的开头写了一些废话并且没有产生错误。更改 httpd.conf 后,我还重新启动了 Apache 服务器。也许问题是我不完全确定我应该把 .htaccess 文件放在哪里。

我的 MAMP 文档根目录以前是 MAMP > htdocs,但我将其更改为 Library > WebServer > Documents,这是我所有 PHP 文件所在的位置。我正在处理的当前项目是这个的子目录,比如 Library > WebServer > Documents > project。这是包含我的 .htaccess 文件的文件夹。我的httpd.conf文件在MAMP > conf > apache中,相关部分如下:

# First, we configure the "default" to be a very restrictive set of 
# features.  
#
<Directory />
    Options Indexes FollowSymLinks
    AllowOverride All
</Directory>

我是否需要将目录从 / 更改为其他目录才能正常工作?非常感谢任何帮助。

编辑:我在下面的评论后将以下 <VirtualHost> 块添加到我的 httpd.conf 中,但同样,似乎没有任何改变:

<VirtualHost *:80>
   ServerAdmin email@site.com
   ServerName localhost:8890

   DocumentRoot /
   <Directory />
      Options FollowSymLinks
      AllowOverride None
   </Directory>
   <Directory />
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
   </Directory>
</VirtualHost>

更新(已解决):我能够通过将 MAMP 的 DocumentRoot 恢复为原始 MAMP > htdocs 而不是我的 [=20] 来解决问题=]. Apache 现在正在读取位于我的 MAMP > htdocs > project 中的 .htaccess 文件。谢谢大家的帮助。

尝试将 Listen 80Listen 443 添加到主配置的顶部。

检查你的httpd.conf。通过 AllowOverride (http://httpd.apache.org/docs/current/mod/core.html#allowoverride) and AllowOverrideList (http://httpd.apache.org/docs/current/mod/core.html#allowoverridelist) 指令,它可以控制是否考虑 .htaccess 个文件。

When this directive (i.e. AllowOverride) is set to None and AllowOverrideList is set to None, .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.

请注意,两者的默认值都是 None,这恰恰导致了这种不考虑 .htaccess 个文件的行为。

首先我会尝试修复我的 VirtualHost 以使用正确的路径。我对 Mac 不是很熟悉,但是如果 Library > WebServer > Documents > project 对应于文件系统中的 /Library/WebServer/Documents/project,那么我会使用它。可能是 /home/your_username/Library/WebServer/Documents/project,但正如我所说,我不是 Mac 人。接下来,我不明白为什么您对具有相反权限的同一目录有指令,所以我会尝试删除第一个目录块或将目标目录更改为其中一个块上的其他目录。请注意,您永远不应使用 apache 授予对 / 的访问权限,因为这将是一个非常大的安全漏洞。

<VirtualHost *:80>
   ServerAdmin email@site.com
   ServerName localhost:8890

   DocumentRoot /Library/WebServer/Documents/project
   <Directory />
      AllowOverride none
      Require all denied
   </Directory>
   <Directory /Library/WebServer/Documents/project>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Require all granted
   </Directory>
</VirtualHost>

如果它仍然不起作用,您可能需要使用 AccessFileName 指令确保 Apache 正在寻找的文件名没有被更改为其他名称。

此外,您没有详细说明放置 VirtualHost 块的位置,但它是否位于单独的文件中。确保以某种方式包含该文件。

您没有指定您的 Apache 版本,但从 2.4 版开始,您应该使用 Require 而不是 Allow 指令,因为它已被弃用。参见 https://httpd.apache.org/docs/2.4/en/howto/access.html