使用index.php而不是index.html是不是一种糟糕的做法


Is it bad practice to use index.php instead of index.html?

我不知道为什么应该这样,但index.html感觉"对"。我使用index.php是因为如果登录失败,我想返回到index.php,并在初始登录页面中显示返回的错误,如下所示。。。

<?php
   if(isset($_GET["loginFailed"])) {
      echo ($_GET['reason']);
   }
?>

这有点删减,但php将破译"原因",并显示一个用户友好的错误。

登录错误只是从我的"login.php"中返回的,类似于。。

if($row === false) {
    die(header("location:index.php?loginFailed=true&reason=usernamenotfound"));
}

这一切都很好,但我必须将索引命名为index.php,而不是index.html。如果我将其更改为index.html,php部分将被忽略。

我的做法有什么问题吗?是不安全还是什么?

使用index.php而不是index.html没有问题。它不会影响搜索引擎排名——所以你想改变它的唯一原因是为了美观。

如果要使用index.html而不是index.php,有两个选项:

首先,您可以使用.htaccess重写扩展。在.htacccess中添加以下行,它们将在不更改URL的情况下将所有对.html页面的请求重新映射到.php页面。

RewriteEngine on
RewriteRule ^(.*)'.html$ $1.php [nc]

其次,您可以允许PHP解释器解析.html文件。要做到这一点,请在.htaccess中添加以下一行(不同的主机有不同的语法,第一行适用于大多数共享主机)

AddHandler application/x-httpd-php5 .html .htm
AddType application/x-httpd-php .html .htm
AddHandler application/x-httpd-php .html .htm
相关文章: