使用自定义 .htaccess 文件的虚拟主机上出现错误 404
P粉729198207
P粉729198207 2023-09-05 10:28:49
0
2
619
<p>我在本地 Linux 服务器上安装了 apache2。它有一个名为 <code>pcts.local</code> 的虚拟主机,其根目录为 <code>/var/www/repos/pcts/</code>。 pcts.local 的根目录内是一个 .htaccess 文件,如果未按如下方式给出,该文件会尝试重写 url 以包含 .php:</p> <pre class="brush:php;toolbar:false;">http://pcts.local/ -> http://pcts.local/index.php http://pcts.local/contact -> http://pcts.local/contact.php</pre> <p>问题是,<code>http://pcts.local/contact</code> 给出错误 404,但 <code>http://pcts.local/contact.php</code> 给出 200。</p> <h3>虚拟主机配置:</h3> <pre class="brush:php;toolbar:false;"><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></pre> <h3>.htaccess 文件在 <code>/var/www/repos/pcts/</code></h3> <pre class="brush:php;toolbar:false;">RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.+)$ $1.php [NC,L]</pre> <p>提前感谢您的帮助!</p>
P粉729198207
P粉729198207

全部回复(2)
P粉151720173

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

试试这个:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .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 覆盖(请参阅下面的参考链接)

参考:

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!