CentOS无法读取htaccess文件。

13
我正在处理一台新服务器,通过yum安装了"Web Server"组件。Php和mysql正常工作,但我无法让 .htaccess 正常工作。
这是我的测试 .htaccess 文件:
WASD_TEST_CALL_ERROR
我把这个放在一个测试文件夹中的 .htaccess 文件里,并且还有一个 index.html 页面。它没有报错,而是继续加载 index 页面而没有显示任何错误。
谢谢。
5个回答

36

请确保 AccessFileName 设置为 .htaccess。

在 httpd.conf 中搜索 AccessFileName 指令。它定义了分发配置文件的名称:

grep -i AccessFileName httpd.conf

确保用户被允许使用.htaccess文件。

允许放入这些文件中的内容取决于AllowOverride指令。该指令按类别指定了将在.htaccess文件中查找时将受到尊重的指令。如果此指令设置为None,则完全忽略.htaccess文件,即服务器甚至不会尝试读取文件系统中的.htaccess文件。

grep -i AllowOverride httpd.conf

如果将此指令设置为 All,则任何具有 .htaccess 上下文的指令都允许在 .htaccess 文件中使用:

AllowOverride All

保存并关闭文件。重新启动 httpd:

service httpd restart

如果你收到了 Failed to restart httpd.service: The name org.freedesktop.PolicyKit1 was not provided by any .service files 的错误提示,则需要执行 sudo httpd restart - Mooze

3

你是否在 Apache 配置中设置了 AllowOverride?如果没有,将 AllowOverride 从 none 更改为 all


3
在CentOS7上,以下内容应该会有所帮助:
  1. /etc/httpd/conf/httpd.conf文件中的字符串AllowOverride controls what directives may be placed in .htaccess files下:将AllowOverride None替换为AllowOverride All

  2. /etc/httpd/conf.modules.d/00-base.conf文件中检查rewrite_module必须是:

    LoadModule rewrite_module modules/mod_rewrite.so

  3. .htaccess文件必须位于/var/www/html/

  4. 示例内容.htaccess:

    AuthUserFile /var/www/.htpasswd AuthGroupFile /dev/null AuthName "Please Enter Password" AuthType Basic Require valid-user
  5. 检查并重启Apache:

    httpd -t

    /bin/systemctl restart httpd.service

0
前往 /etc/httpd/conf/httpd.conf,找到以下行。在 .htaccess 文件中允许 htaccess 上下文:AllowOverride All。
保存并关闭文件。重新启动 httpd: enter image description here

1
在展示代码时,请嵌入实际的代码而不是代码图像。 - herrbischoff

0
  1. 将/etc/httpd/conf/httpd.conf中的AllowOverride None替换为AllowOverride All

  2. 运行httpd -M | grep rewrite_module命令,结果应该如下:

    enter image description here

rewrite_module (shared)

即使确保了上述所有内容,仍然无法正常工作?那么请确保您的.htaccess文件顶部有以下行。

RewriteEngine On

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接