php - 包含 PHP 的语句不要访问 .htaccess
问题描述
通过将 .htaccess 添加到根文件夹来使用 Pretty URLS 时,我的<a href="">
链接当然不需要.php
文件扩展名。但是,当我使用 PHPrequire
或include
时,它会引发错误并且似乎没有使用 .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?
解决方案
Apache Web 服务器对网络上的请求作出反应。RewriteRules 仅适用于这些请求。
PHP require
, include
, file_get_contents
, ... 函数通常访问本地存储,而不是通过网络向 Apache 发出 HTTP 请求。
虽然使用file_get_contents
URL 是可能的并且有时很有用,但在您的用例中,您必须添加.php
扩展名以寻址正确的本地文件。
推荐阅读
- sql-server - 在 DB2 数据库中转换 CROSS APPLY
- visual-studio-code - 如何在命令提示符下运行 VS Code 程序?
- google-app-engine - 区域 europe-west3 没有足够的资源来满足请求。请稍后再试
- c++ - Tesseract 交叉编译 aarch64 (arm64-v8a) 错误此标头仅适用于 x86
- sql - SQL 数据库关系:在多对多关系中正确实现连接模型?
- typescript - TypeScript 和 Swagger:创建通用的 GetManyAndCount 类定义
- python - 无法使用python从存储过程中获取数据
- python - 绘制相同 bin 大小但每个 bin 中的值不同的直方图
- android - 模拟器不启动(连接到模拟器)
- python - Django Rest Framework:路由器不工作