简体   繁体   English

使用index.php而不是index.html是不好的做法吗?

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

I'm not sure why it should be, but index.html feels 'right'. 我不确定为什么会这样,但index.html感觉“正确”。 I'm using index.php because if my login fails, I want to return to index.php and show the returned error on in the initial log-in page, like this... 我正在使用index.php,因为如果我的登录失败,我想返回index.php并在初始登录页面中显示返回的错误,就像这样......

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

That's trimmed a bit, but the 'reason' will be deciphered by the php and a user-friendly error shown. 修剪了一下,但是“原因”将被php破译并显示出用户友好的错误。

The login error is just returned from my 'login.php' with something like.. 登录错误只是从我的'login.php'返回,类似于..

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

This all works fine, but I have to keep the index named as index.php instead of index.html. 这一切都很好,但我必须保持索引名为index.php而不是index.html。 If I change it to index.html, the php part is just ignored. 如果我将其更改为index.html,则只会忽略php部分。

Is there anything wrong with my approach? 我的方法有什么问题吗? Is it unsafe or anything? 是不安全还是其他什么?

No issue with using index.php instead of index.html . 使用index.php而不是index.html没有问题。 It does not affect search engine rankings - So the only reason you would want to change it is for aesthetics. 它不会影响搜索引擎排名 - 所以你想要改变它的唯一原因是美学。

If you want to use index.html instead of index.php , there are two options: 如果要使用index.html而不是index.php ,有两个选项:

Firstly, you can rewrite extensions using .htaccess. 首先,您可以使用.htaccess重写扩展名。 Add the following lines to your .htaccess, and they will remap all requests for a .html page to a .php one without the URL changing. 将以下行添加到.htaccess中,他们会将.html页面的所有请求重新映射到.php页面而不更改URL。

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

Secondly, you can allow .html files to be parsed by the PHP interpreter. 其次,您可以允许PHP解释器解析.html文件。 To do this, add one of the following lines to your .htaccess (Different hosts have different syntax, the first works for most shared hosts) 为此,请将以下行之一添加到.htaccess(不同的主机具有不同的语法,第一个适用于大多数共享主机)

AddHandler application/x-httpd-php5 .html .htm

AddType application/x-httpd-php .html .htm

AddHandler application/x-httpd-php .html .htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM