仅在 example.com 和 example.com/index.html 而非 example.com/index.php 处访问 codeigniter 网站主页

Access codeigniter website homepage only at example.com and example.com/index.html and not at example.com/index.php

我只需要在 example.com 和 example.com/index.html 访问 codeigniter 网站主页,而不是在 example.com/index.php,我尝试使用 htacess 将 index.php 重定向到 index.html,但无法访问主页。我不想显示它是一个 php 网站。 尽管我可以通过在 application/config/routes.php

中添加下面的示例来显示主页。com/index.html
$route['index.html'] = 'home/index';

但是示例。com/index.php 仍然可以访问,我想重定向到 404

尝试在 .htaccess 文件的顶部添加以下内容:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php$ / [R=302,L]

这会将 /index.php 的任何 直接 请求重定向到 /

检查 REDIRECT_STATUS 环境变量的条件确保我们只重定向直接请求,而不是由 Codeigniter front-controller(稍后出现在 .htaccess 文件中)重写请求。否则,您将收到 rewrite-loop(500 内部服务器错误响应)。

REDIRECT_STATUS env var 未在客户端的初始请求中设置(即评估为空字符串,^$)。但是,当Codeigniter将请求重写为front-controller为index.php时,REDIRECT_STATUS设置为字符串200(代表一个200 OK HTTP状态),所以如上条件当 Codeigniter 将请求重写为 index.php.

时,匹配失败且请求未重定向

您也可以使用上述方法重定向到 index.html,但必须按照您的建议在 Codeigniter 中将 index.html 设置为路由。

But example.com/index.php is still accessible which I want to redirect to 404

如果你想提供 404(尽管重定向,如上所述,会更可取)然后只需将 RewriteRule 更改为读取(保持 condition和以前一样):

RewriteRule ^index\.php$ - [R=404]