使用自定义 .htaccess 文件的虚拟主机上出现错误 404
P粉729198207
P粉729198207 2023-09-05 10:28:49
[PHP讨论组]

我在本地 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]

提前感谢您的帮助!

P粉729198207
P粉729198207

全部回复(2)
P粉151720173

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

试试这个:

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

如果这是您的完整配置,则您的 .htaccess 文件不会被处理。

您尚未启用特定目录的 .htaccess 覆盖。 (即,您尚未启用 .htaccess 文件的解析。)默认情况下,.htaccess 覆盖处于禁用状态。

但是,您也没有启用对文件系统此区域的访问?您是否在服务器配置的其他地方完成了此操作?!

您应该在 容器内有一个相关的 部分,如下所示:

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

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

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

参考:

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号