【问题标题】:how to make default page home.php instead of index.html and index.php如何制作默认页面 home.php 而不是 index.html 和 index.php
【发布时间】:2013-03-24 15:18:38
【问题描述】:

我有网站http://mywebsite.com 如果我点击此 URL,它将 index.php 和 index.html 作为默认页面。如何将 home.php 作为默认页面。 我已经尝试过了,但通过将以下代码放在 public_html 的 .htaccess 文件中没有工作

DirectoryIndex home.php index.html index.php

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    您只需要在您的DirectoryIndex 中添加home.php 即可使其正常工作。请记住,这是在根项目的 .htaccess 文件中使用的:

    DirectoryIndex home.php
    

    【讨论】:

    • 它适用于所有子文件夹。我只想应用到根文件夹。
    • 注意:此代码将适用于您的所有子文件夹。使用下面的代码而不是 'DirectoryIndex' 。 >>>>>>>> RewriteRule 上的 RewriteEngine ^$ /yourfile.html [L]
    【解决方案2】:

    您需要在httpd.conf 中使用AllowOverride +Indexes 才能在.htaccess 中使用DirectoryIndex

    除此之外,最简单的重定向方法(没有对 Apache 配置和模块的 root 访问权限)是将其设置为 index.html

    <!doctype html>
    <html>
      <head>
        <meta http-equiv="Refresh" content="0; url=home.php">
      </head>
      <body>
      </body>
    </html>
    

    【讨论】:

    • 但是哪种方式更好呢?哪个更快?直接访问 home.html 而不是从一个 url 重定向到另一个对用户来说不是更好吗?
    • @PrzemysławNiemiec 是的。第二种解决方案是当您无法控制 Apache 时的解决方法,如答案中所述。
    【解决方案3】:

    DirectoryIndex 指令适用于所有子文件夹,如果你想为每个目录设置不同的文件,你可以使用 mod-rewrite。

    要将 /file.html 设置为根目录处理程序,您可以在 htaccess 顶部使用它:

    RewriteEngine on
    RewriteRule ^$ /file.html [L]
    

    要将不同的文件设置为子文件夹的索引,请使用:

    RewriteEngine on
    RewriteRule ^subfolder/$ /myfile.html [L]
    

    【讨论】:

      【解决方案4】:

      只需尝试将/index.html/index.php 重写为/home.php

      Options +FollowSymlinks
      RewriteEngine on
      
      RewriteCond %{REQUEST_URI} ^/index\.(html|php)
      RewriteRule ^(.*) /home.php
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-19
        • 2012-12-21
        • 2014-09-06
        • 2013-04-18
        相关资源
        最近更新 更多