首页 > 解决方案 > 包含 PHP 的语句不要访问 .htaccess

问题描述

通过将 .htaccess 添加到根文件夹来使用 Pretty URLS 时,我的<a href="">链接当然不需要.php文件扩展名。但是,当我使用 PHPrequireinclude时,它会引发错误并且似乎没有使用 .htaccess 添加必要的 .php,需要我将 .php 添加到这些语句中。

.htaccess

# Redirect everything that doesn't match a directory or file to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

有问题的 .php 文件:

<!DOCTYPE html>
<head></head>
<body>
    <?php require "../Resources/nav.php";?>
        // Main Content
    <?php require "../Resources/footer.php";?>
</body>

有谁知道为什么这些语句不使用.htaccess?是否只需要包含文件扩展名,还是有办法强制他们使用.htaccess?

标签: php.htaccess

解决方案


Apache Web 服务器对网络上的请求作出反应。RewriteRules 仅适用于这些请求。

PHP require, include, file_get_contents, ... 函数通常访问本地存储,而不是通过网络向 Apache 发出 HTTP 请求。

虽然使用file_get_contentsURL 是可能的并且有时很有用,但在您的用例中,您必须添加.php扩展名以寻址正确的本地文件。


推荐阅读