将对PHP应用程序的请求重定向到文本文件


Redirecting requests to a PHP application to a text file

我想把所有到我网站的请求重定向到。txt文件,所以我找到了两种方法,

方式1:

重定向到一个php文件,该文件将显示如下所示的。txt文件内容。

. htaccess

# Enable rewrite engine and route requests to framework
RewriteEngine On
# Some servers require you to specify the `RewriteBase` directive
# In such cases, it should be the path (relative to the document root)
# containing this .htaccess file
#
# RewriteBase /
RewriteCond %{REQUEST_URI} '.ini$
RewriteRule '.ini$ - [R=404]
RewriteCond %{REQUEST_URI} '.html?$
RewriteRule '.html?$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

index . php

<?php
header('Content-type: text/plain');
header('Content-Disposition: inline; filename="details.txt"');
readfile('details.txt');
?>

方式2:

使用。htaccess直接重定向到。txt文件

. htaccess

DirectoryIndex details.txt index.php

所以两者都给我相同的结果,还有我遗漏的其他区别吗?哪条路是最好的?我需要立即帮助,谢谢。

方法1:您可以使用php执行任何命令,然后转到txt文件。它也会调用php引擎。

方法二:直接调用txt文件。这样可以节省服务器负载。但是你不能执行任何命令。

由于路1将通过两个引擎,所以它会很慢,但你可以使用它作为你喜欢的。另一方面,方法2将更快,但没有来源。

所以你更喜欢哪种方式取决于你的项目。

选择Way2 '。直接重定向到.txt文件

由于不需要PHP解析,它将更快,更高效。在第二种方法中,PHP处理程序应该解析脚本,打开文本文件并显示它。所以Way2肯定更好。