【问题标题】:index.php not loading by defaultindex.php 默认不加载
【发布时间】:2011-01-23 23:22:45
【问题描述】:

我刚刚安装了 CentOS、Apache 和 PHP。当我访问我的网站http://example.com/myapp/ 时,它显示“禁止”。默认情况下它不会加载 index.php 文件。

当我访问http://example.com/myapp/index.php 时,它工作正常。

知道如何解决这个问题吗?

【问题讨论】:

    标签: php apache centos apache-config


    【解决方案1】:

    Apache 需要配置为将 index.php 识别为索引文件。

    实现这一点的最简单方法..

    1. 在您的 Web 根目录中创建一个 .htaccess 文件。

    2. 添加行...

    DirectoryIndex index.php

    这是有关此事的资源...
    http://www.twsc.biz/twsc_hosting_htaccess.php

    编辑:我假设 apache 配置为允许 .htaccess 文件。如果不是,则必须修改 apache 的配置文件 (httpd.conf) 中的设置

    【讨论】:

    • 应该在apache加载的php.conf文件中。
    • 我认为你的意思是 php.ini。无论如何,他的 apache 没有将 index.php 识别为目录索引文件。它处理 php 文件是否是另一个 apache 配置问题。
    • 别忘了重启apache!!和我一样! ://
    • 除非必要,否则最好不要使用.htaccess 文件(即,如果您是共享主机环境中的用户并且.htaccess 是设置自定义配置选项的唯一方法,那么这个是唯一的解决方案,但是当您具有管理访问权限时这样做会导致安全风险显着的性能损失。如果您有访问权限,则相同的指令可以进入httpd.conf。有这样做也有一些缺点;如果项目不使用 php,那么index.php 文件将不存在。
    • 要添加到@staticsan 的注释中,请确保您也启用了 php:在 httpd.conf 中取消注释相应的 LoadModule 行(例如,LoadModule php7_module ...)
    【解决方案2】:

    虽然将“DirectoryIndex index.php”添加到 .htaccess 文件可能会起作用,

    注意:

    一般来说,你不应该使用 .htaccess 文件

    引用自http://httpd.apache.org/docs/1.3/howto/htaccess.html
    虽然这里指的是旧版本的 apache,但我相信这个原则仍然适用。

    将以下内容添加到您的 httpd.conf(如果您可以访问它)被认为是更好的形式,导致更少的服务器开销并且具有完全相同的效果:

    <Directory /myapp>
    DirectoryIndex index.php
    </Directory>
    

    编辑: 在编辑时,v1.3 文档已关闭。 v2.4 documentation(编辑时的当前版本)有类似的立场:

    一般来说,应尽可能避免使用.htaccess 文件。您考虑放入 .htaccess 文件的任何配置,都可以在主服务器配置文件的 &lt;Directory&gt; 部分中进行。

    【讨论】:

    • 链接失效
    • @Rhdr:感谢您指出这一点。帖子已更新为 v2.4 文档的新链接,因此不应再失效。
    【解决方案3】:

    我猜我会说目录索引设置为 index.html,或者一些变体,试试:

    DirectoryIndex index.html index.php
    

    这仍然会使 index.html 优先于 index.php(如果您需要抛出维护页面,则很方便)

    【讨论】:

    • 我的看起来像这样,但不幸的是下载 index.php 而不是执行它。
    • @Webnet 那么您应该考虑将 Type 和 LoadModules 更改为 php,以便它读取 php [stackoverflow.com/questions/5121495/…
    【解决方案4】:

    这可能对某人有帮助。 这是来自 httpd.conf (Apache version 2.2 windows) 的 sn-p

    # DirectoryIndex: sets the file that Apache will serve if a directory
    # is requested.
    #
    <IfModule dir_module>
        DirectoryIndex index.html
        DirectoryIndex index.php
    </IfModule>
    

    现在这将寻找 index.html 文件,如果找不到,它将寻找 index.php。

    【讨论】:

      【解决方案5】:

      这篇文章可能很旧,但我只是发布它以防它帮助其他人,我不建议在您的 Web 根目录中创建一个 .htaccess 文件并更改索引。我觉得最好按照步骤进行

      1. 转到我的apache文件夹的conf文件夹

        C:\Apache24\conf

      2. 打开名为

        的文件

        httpd.conf

      3. 转到该部分

        <IfModule dir_module>
           DirectoryIndex index.html 
        
         </IfModule>
        
      4. 如下图添加index.php

         <IfModule dir_module>
          DirectoryIndex index.html index.php
        
        </IfModule>
        

      这样,它仍然选择 index.html 和 index.php 作为默认索引,但优先考虑 index.html,因为 index.html 在 *index.php 之前。我的意思是你在同一个目录中同时拥有 index.html 和 index.php,index.html 将被用作默认索引,除非你在 index.hml之前写 **index.php* >

      我希望它可以帮助某人......快乐编码

      【讨论】:

      • 这是所有这些答案中唯一对我有用的东西。非常感谢!
      【解决方案6】:

      尝试使用以下内容创建一个 .htaccess 文件

      DirectoryIndex index.php
      

      编辑:实际上,是不是有一个 'php-apache' 包或你应该与它们一起安装的东西?

      【讨论】:

        【解决方案7】:

        在阅读了所有这些并尝试修复它之后,我在 ubuntu 论坛 (https://help.ubuntu.com/community/ApacheMySQLPHP) 上得到了一个简单的解决方案。问题在于 libapache2-mod-php5 模块。这就是浏览器下载 index.php 文件而不是显示网页的原因。请执行下列操作。如果 sudo a2enmod php5 返回模块不存在,则问题出在 libapache2-mod-php5。使用命令清除模块 sudo apt-get --purge remove libapache2-mod-php5 然后再次安装 sudo apt-get install libapache2-mod-php5

        【讨论】:

          【解决方案8】:

          这个就像一个魅力!

          第一

          <IfModule dir_module>
              DirectoryIndex index.html
               DirectoryIndex index.php
          </IfModule>
          

          然后从

          <Files ".ht*">
              Require all denied
          </Files>
          

           <Files ".ht*">
              Require all granted
          </Files>
          

          【讨论】:

            【解决方案9】:

            我有类似的症状。不过,就我而言,我的白痴无意中在网络根文件夹中也有一个空的 index.html 文件。当我没有明确请求 index.php 时,Apache 提供了这个而不是 index.php,因为 DirectoryIndexmods-available/dir.conf 中配置如下:

            DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
            

            也就是说,“index.html”在优先级列表中出现在“index.php”之前。从 Web 根目录中删除 index.html 文件自然可以解决问题。哦!

            【讨论】:

              猜你喜欢
              • 2017-10-25
              • 2014-02-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-02-03
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多