同时使用索引.html和索引.php时,从 URL 中隐藏索引.php


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

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

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

修改 .htaccess 不起作用,我认为那里的任何代码都没有做我想要的(而且我自己还没有设法编写一个)。我尝试过将iFrame与JavaScript一起使用,但很难找到可行的解决方案。我来的关闭是使用目录索引作为索引.php但当然索引.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 语句。虽然我从未尝试过,但我认为这就是你要找的。

此链接可能还有一些额外的提示,介绍如何将您正在使用的扩展程序屏蔽为它们不是。

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

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

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

鉴于您同时具有index.htmlindex.php这只有在您使用Cyclone的应答从index.php内部呼叫index.html并且不会再次出现时才有效。