当我通过编辑.htaccess文件获得404时,如何返回200?

wei*_*lou 1 .htaccess url-rewriting http-status-codes http-status-code-404 http-status-code-200

我在域的根目录中有一个.htaccess文件和一个index.html文件.我在下面的.htaccess文件中编码:

DirectorySlash Off

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])?$ /index.html [L]
Run Code Online (Sandbox Code Playgroud)

我希望用户在访问目录URL时会看到index.html中的内容,无论此目录URL是否有斜杠.所以我上面有代码.

但我有一个问题.当我访问不存在的目录时.我得到404.这不是我想要的.我希望用户在访问目录URL时永远可以获得200并在index.html中查看内容.如何通过编辑.htaccess文件中的代码来解决此问题?

BTW.请不要将用户重定向到index.html文件.

谢谢!

Gai*_*aia 6

创建一个自定义404页面并将其命名为"404page.php".在将任何其他内容发送到浏览器之前,请确保此页面发送200状态,方法是将其放在顶部:

<?php
header("Status: 200 OK");
?>
Run Code Online (Sandbox Code Playgroud)

然后输入ErrorDocument 404 /404page.php你的htaccess.