如何使默认页面home.php代替index.html和index.php

如何使默认页面home.php代替index.html和index.php,php,.htaccess,Php,.htaccess,我有一个网站,如果我点击这个URL,它会以index.php和index.html作为默认页面。如何使home.php成为默认页面。 我尝试过这一点,但在public_html的.htaccess文件中放置了以下代码,结果无效 DirectoryIndex home.php index.html index.php 您只需在DirectoryIndex中使用home.php,即可使其正常工作。请记住,这是在根项目的.htaccess文件中使用的: DirectoryIndex home.php

我有一个网站,如果我点击这个URL,它会以index.php和index.html作为默认页面。如何使home.php成为默认页面。 我尝试过这一点,但在public_html的.htaccess文件中放置了以下代码,结果无效

DirectoryIndex home.php index.html index.php

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

DirectoryIndex home.php

您需要
httpd.conf
中的
AllowOverride+索引
,才能在
.htaccess
中使用
DirectoryIndex

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

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

试着把
/index.html
/index.php
重写成
/home.php

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/index\.(html|php)
RewriteRule ^(.*) /home.php

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

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

RewriteEngine on
RewriteRule ^$ /file.html [L]
要将不同的文件设置为子文件夹的索引,请使用以下命令:

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

它适用于所有子文件夹。我只想应用于根文件夹。注意:此代码将应用于您的所有子文件夹。请使用以下代码而不是“DirectoryIndex”。>>>>>>>>RewriteRule上的RewriteEngine^$/yourfile.html[L]但哪种方式更好?哪一个更快?对于用户来说,直接转到home.html而不是从一个url重定向到另一个url不是更好吗?@PrzemysławNiemiec是的,是的。第二个解决方案是解决当您无法控制Apache时的问题,如答案中所述。