我在本地 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>
/var/www/repos/pcts/RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [NC,L]
提前感谢您的帮助!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
在您的代码中,REQUEST_FILENAME 需要一个带有 php 扩展名的文件来执行重写。
试试这个:
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]如果这是您的完整配置,则您的
.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覆盖(请参阅下面的参考链接)参考: