gpt4 book ai didi

php - Apache 使用它自己的 404 处理程序而不是 index.php 来处理 .txt 文件

转载 作者:行者123 更新时间:2023-12-05 02:33:58 25 4
gpt4 key购买 nike

我想通过 .php 处理对不存在文件的所有请求

.htaccess 配置:

ErrorDocument 404 /index.php
DirectoryIndex index.php
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

这似乎大部分工作,但一些特定的文件扩展名(.txt、.jpg,还有更多?)由 apache 处理,而不是通过 php 传递。
http://localhost:8000/home -> 显示主页
http://localhost:8000/file.zip(不存在)-> 显示由 PHP 提供的自定义样式的 404 页面

http://localhost:8000/exists.txt(现有文件)-> 提供现有文件
http://localhost:8000/doesnotexist.txt(不存在)-> 显示默认的 apache 404 页面(与未安装 PHP 时相同)

是否有特定文件扩展名的默认 apache 处理程序?
如何将所有请求设置为通过 index.php 传递?

使用 docker php 7.4 apache as my base image

最佳答案

我复制了您使用 Docker 和 php 7.4-apache 镜像进行的实验。当打开 url http://localhost:8000/doesnotexist.txt 时,我将按预期转到 index.php 文件。底部的三个重写规则将所有内容重写到 index.php。

首先:您还没有发布 docker-compose.ymlDockerfile,所以可以肯定的是:您必须通过命令为 Apache 启用重写在 dockerfile 或自定义 Dockerfile 中,如下所示:

# Dockerfile
FROM php:7.4-apache
RUN a2enmod rewrite

如果您没有启用重写,则会出现错误 500,因为重写规则周围没有 if 条件。

如果你确实启用了重写,行为仍然很奇怪,因为即使 ErrorDocument 404/index.php 行不存在,底部重写应该使 index.php 无论如何都被提供.换句话说:.htaccess 中的底部三行将确保不提供 404,因为所有内容都被重写到 index.php。

当您说“默认的 apache 404 页面”时,您指的是“未找到”页面吗?

Not Found (H1 tag)
The requested URL was not found on this server.

Apache/2.4.52 (Debian) Server at localhost Port 8000

尝试删除并重建容器,这样就不会出现奇怪的缓存问题。

那你的 index.php 呢?那里有什么东西可以触发奇怪的东西吗?你能把它贴在这里吗?

关于php - Apache 使用它自己的 404 处理程序而不是 index.php 来处理 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70849141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com