当同时使用 index.html 和 index.php 时从 URL 中隐藏 index.php

Hide index.php from URL when using both index.html and index.php

我有一个同时使用 index.html 和 index.php 的网站。 index.html 是一个年龄限制启动页面,当您在按钮上单击“是”时,您会使用 index.php 重定向到主站点,这是一个没有任何 /subdomains 的单页滚动条。

我想做的是,当您被重定向到 index.php 时,隐藏示例的 index.php。com/index.php URL ...我搜索了整个网络,但尚未找到有效的解决方案。

修改 .htaccess 没有用,我认为没有任何代码可以满足我的要求(而且我自己也没有设法编写一个)。我试过将 iFrame 与 JavaScript 一起使用,但很难找到可行的解决方案。我关闭的是使用 DirectoryIndex for index.php 但当然 index.html 将不起作用。

我确信对此有一个简单的解决方案,但我的想法是错误的。我将如何解决这个问题?

感谢您的宝贵时间!

一种方法是将以下内容添加到您的 .htaccess 文件中:

DirectoryIndex index.php index.html

这将使用 index.php 作为默认索引文件。在 php 文件中,您可以执行类似这样的操作以在用户第一次访问您的站点时显示 index.html

// start a new session and check if the index.html have been included
session_start();
if(!isset($_SESSION['index_included'])) {
  // display the index.html and exit script
  // notice that we are setting index_included to true in our session
  include_once('index.html');
  $_SESSION['index_included'] = 1;
  exit;
}
// rest of your php goes here

如果您想标记用户已单击 index.html 中的按钮,您可以使用 ajax:

<!DOCTYPE html>
<html>
<head>
<title>Sample Index.html</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
// index.html
$(document).ready(function() {
  //
  $('#button').click(function() {
    // make an ajax request to index.php to tell that the button
    // is clicked
    $.ajax('index.php', {
      type : 'post',
      data : 'clicked=true',
      success:function() {
        // reload the location to send user to index.php
        window.location.reload();
      }
    });
    return false;
  });
});
</script>
<body>
  <a href="#" id="button">click me</a>
</body>
</html>

然后在 index.php 中捕获 ajax 调用并像这样设置会话变量:

// index.php
session_start();
// if we receive an ajax request we mark this in the session
if(isset($_POST['clicked'])) {
   $_SESSION['clicked'] = true;
   exit;
}
if(!isset($_SESSION['clicked'])) {
  // since the user hasn't clicked the button in index.html yet
  // we include that file 
  include_once('index.html');
  exit;
}
// rest of your php code which only gets executed if $_SESSION['clicked'] is set

作为替代方案,您可以使用 setcookie() 而不是将状态存储在会话中。这样的事情应该有效:

// index.php
if(isset($_POST['clicked'])) {
  setcookie('clicked', true);
  exit;
}
if(!isset($_COOKIE['clicked'])) {
  include_once('index.html');
  exit;
}
// rest of index.php

您可以查看 php.ini 文件并添加 expose_php = off 语句。 虽然我从来没有尝试过,但我想这就是你要找的。

link 可能还有一些额外的提示,说明如何将您正在使用的扩展程序隐藏起来。

-----更新-----

在阅读了 Cyclone 给出的答案后,由于它代表了一个与我认为您的问题完全不同的问题的解决方案,我再次阅读了您的问题并觉得我给了您错误的答案。但是我会让它留在那儿,以防我第二次看错。

因此,如果您试图从 link http://example.com/index.php 中隐藏 index.php,则没有真正的方法可以使它完全消失,因为用户可能会输入它或从那里获取a link 已经写好了。但是您可以重定向到 http://example.com/ 而不是 http://example.com/index.php.

鉴于你同时拥有 index.htmlindex.php 这只有在你使用 Cyclone 的回答从 index.php 中调用 index.html 并且它不会再次出现时才有效时间。