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