【问题标题】:Error 404 being given on a virtaul host with custom .htaccess file在具有自定义 .htaccess 文件的虚拟主机上出现错误 404
【发布时间】:2022-12-03 01:28:45
【问题描述】:

我在我的本地 linux 服务器上安装了 apache2。它有一个名为pcts.local 的虚拟主机,其根目录为/var/www/repos/pcts/。在 pcts.local 的根目录中是一个 .htaccess 文件,如果没有像下面这样给出,它会尝试重写 url 以包含 .php:

http://pcts.local/ -> http://pcts.local/index.php
http://pcts.local/contact -> http://pcts.local/contact.php

问题是,http://pcts.local/contact 给出错误 404 但 http://pcts.local/contact.php 给出 200。

虚拟主机配置:

<VirtualHost *:80>
        ServerName pcts.local
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/repos/pcts

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

.htaccess 文件在/var/www/repos/pcts/

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [NC,L]

在此先感谢您的帮助!

【问题讨论】:

  • 那是您完整的 VirtualHost 配置吗?

标签: php linux .htaccess apache2 vhosts


【解决方案1】:

在您的代码中,REQUEST_FILENAME 需要一个带有 php 扩展名的文件来执行重写。

试试这个:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

【讨论】:

  • 我刚才试过了,但恐怕我仍然得到 404。
【解决方案2】:
<VirtualHost *:80>
    ServerName pcts.local
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/repos/pcts

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

如果这是您的完整配置,那么您的.htaccess 文件没有被处理。

您尚未为特定目录启用 .htaccess 覆盖。 (即,您没有启用对.htaccess 文件的解析。)默认情况下禁用.htaccess 覆盖。

但是,您也没有启用对文件系统的这个区域的访问吗?你在服务器配置的其他地方做过吗?!

您应该有一个相关的 &lt;Directory&gt; 部分,如下所示里面&lt;VirtualHost&gt; 容器:

<Directory /var/www/repos/pcts>
    # Enable .htaccess overrides
    AllowOverride All

    # Allow user access to this directory
    Require all granted
</Directory>

如果需要,您可以进一步限制 .htaccess 覆盖(参见下面的参考链接)

参考:

【讨论】:

    猜你喜欢
    • 2014-11-29
    • 2015-07-09
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2014-07-30
    相关资源
    最近更新 更多