.htaccess 文件未被读取

.htaccess file Not Being Read

我的 .htaccess 文件没有被读取,我在 /var/www/html 中放置了一个 .htaccess 文件,其中包含

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

然后我的 http.conf 文件看起来像这样

ServerRoot "/etc/httpd"
<Directory />
AllowOverride none
Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>

我想访问的文件在我的 /var/www/html 文件夹中,但是当我尝试从另一个服务器访问它时,我不断收到此错误

Image from origin 'http://serverB' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.

为什么我的 .htacces 没有被读取,我怎样才能让它被读取?

谢谢

服务器上是否启用了mod_headers?使用此命令检查:

apache2ctl -M

如果没有,请使用此命令启用它:

sudo a2enmod headers

之后重启apache服务器。

如果仍然无效,请查看以下答案:

尝试将此行添加到 .htaccess 文件。

每当我使用反向代理时,我都面临同样的问题,因为它是我的开发机器,我只是将这些行添加到 .htaccess 文件中,它解决了这个问题。

<FilesMatch "\.(php)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

您还需要在 /html 文件中允许 AllowOverride All,我不确定为什么,但我遇到了同样的问题,请确保在保存 [=12] 后重新启动 apache =] 文件,否则更改将不适用。

还要确保您在其他任何地方都没有任何其他 .htaccess 文件,因为使用 add 可能会增加 headers 额外时间导致错误。