Html 如何更改 Apache 中的默认索引页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19322345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:21:51  来源:igfitidea点击:

How do I change the default index page in Apache?

htmlapacheindexingdefault

提问by Tyler Montney

I would like to change the default web page that shows up when I browse my site. I currently have a reporting program running, and it outputs a file called index.html. I cannot change what it calls the file. Therefore, my landing page must be called something else. Right now, when I browse my site it takes me to the reporting page.

我想更改浏览网站时显示的默认网页。我目前正在运行一个报告程序,它输出一个名为 index.html 的文件。我无法更改它对文件的调用。因此,我的着陆页必须称为其他名称。现在,当我浏览我的网站时,它会将我带到报告页面。

From what I see, whatever you call index.html it will pull that up as your default. I want to change that to landing.html. How do I do this?

据我所知,无论您调用 index.html 什么,它都会将其作为您的默认值。我想把它改成landing.html。我该怎么做呢?

I am a folder (Folding @ Home). The reporting program is HFM.net. HFM can output an html file with my current folding statistics. It names the html file index. I do not want that to be my default home page. I would like a menu-like landing where I can choose if I want to see my stats, or something else. The website is at /home/tyler/Documents/hfm/website (landing.html and hfm's index.html are here). Apache2 is in its default directory.

我是一个文件夹(折叠@首页)。报告程序是 HFM.net。HFM 可以输出带有我当前折叠统计信息的 html 文件。它命名 html 文件索引。我不希望它成为我的默认主页。我想要一个类似菜单的着陆点,我可以在那里选择是否要查看我的统计数据或其他内容。该网站位于 /home/tyler/Documents/hfm/website(landing.html 和 hfm 的 index.html 在这里)。Apache2 位于其默认目录中。

I'm also running Ubuntu 13.04.

我也在运行 Ubuntu 13.04。

回答by PlantTheIdea

I recommend using .htaccess. You only need to add:

我建议使用.htaccess. 您只需要添加:

DirectoryIndex home.php

or whatever page name you want to have for it.

或您想要的任何页面名称。

EDIT: basic htaccess tutorial.

编辑:基本的 htaccess 教程。

1) Create .htaccessfile in the directory where you want to change the index file.

1).htaccess在要更改索引文件的目录中创建文件。

  • no extension
  • .in front, to ensure it is a "hidden" file
  • 没有延期
  • .在前面,以确保它是一个“隐藏”文件

Enter the line above in there. There will likely be many, many other things you will add to this (AddTypes for webfonts / media files, caching for headers, gzip declaration for compression, etc.), but that one line declares your new "home" page.

在那里输入上面的行。您可能会添加很多很多其他内容(用于 web 字体/媒体文件的 AddTypes、用于标题的缓存、用于压缩的 gzip 声明等),但是这一行声明了您的新“主页”页面。

2) Set server to allow reading of .htaccessfiles (may only be needed on your localhost, if your hosting servce defaults to allow it as most do)

2)将服务器设置为允许读取.htaccess文件(如果您的托管服务默认允许读取文件,则可能仅在您的本地主机上需要)

Assuming you have access, go to your server's enabled site location. I run a Debian server for development, and the default site setup is at /etc/apache2/sites-available/defaultfor Debian / Ubuntu. Not sure what server you run, but just search for "sites-available" and go into the "default" document. In there you will see an entry for Directory. Modify it to look like this:

假设您有权访问,请转到服务器的启用站点位置。我运行 Debian 服务器进行开发,默认站点设置是/etc/apache2/sites-available/defaultDebian / Ubuntu。不确定您运行的是什么服务器,只需搜索“可用站点”并进入“默认”文档。在那里你会看到一个目录条目。将其修改为如下所示:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Then restart your apache server. Again, not sure about your server, but the command on Debian / Ubuntu is:

然后重启你的apache服务器。同样,不确定您的服务器,但 Debian / Ubuntu 上的命令是:

sudo service apache2 restart

Technically you only need to reload, but I restart just because I feel safer with a full refresh like that.

从技术上讲,你只需要重新加载,但我重新启动只是因为我觉得像这样完全刷新更安全。

Once that is done, your site should be reading from your .htaccess file, and you should have a new default home page! A side note, if you have a sub-directory that runs a site (like an admin section or something) and you want to have a different "home page" for that directory, you can just plop another .htaccessfile in that sub-site's root and it will overwrite the declaration in the parent.

完成后,您的站点应该正在读取您的 .htaccess 文件,并且您应该有一个新的默认主页!附带说明,如果您有一个运行站点的子目录(例如管理部分或其他内容)并且您希望该目录有一个不同的“主页”,则可以.htaccess在该子站点的根目录中放置另一个文件它将覆盖父级中的声明。

回答by botheredbybees

You can also set DirectoryIndex in apache's httpd.conf file.

您还可以在 apache 的 httpd.conf 文件中设置 DirectoryIndex。

CentOS keeps this file in /etc/httpd/conf/httpd.confDebian: /etc/apache2/apache2.conf

CentOS 将这个文件保存在/etc/httpd/conf/httpd.confDebian 中:/etc/apache2/apache2.conf

Open the file in your text editor and find the line starting with DirectoryIndex

在文本编辑器中打开文件并找到以 DirectoryIndex 开头的行

To load landing.html as a default (but index.html if that's not found) change this line to read:

要将landing.html 加载为默认值(但如果未找到,则加载index.html)将此行更改为:

DirectoryIndex  landing.html index.html