如何让网站运行 index.html 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22121056/
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
How to make a website run the index.html file?
提问by Anonymous
I just hosted a website. But, when i want to run the website it opens the index of the website (list of files what i added using FTP and not the Index.html page). How to fix that?
我刚刚托管了一个网站。但是,当我想运行该网站时,它会打开该网站的索引(我使用 FTP 添加的文件列表,而不是 Index.html 页面)。如何解决?
Please help...
请帮忙...
回答by Adriano Monecchi
If you want to display the contents of an index.html file when a request is made to your website domain, let's say it is domain.com, then all you have to do is to upload the index.html file to the correct document root directoryon your server.
如果您想在向您的网站域发出请求时显示 index.html 文件的内容,假设它是 domain.com,那么您所要做的就是将 index.html 文件上传到正确的文档根目录服务器上的目录。
The document rootis the folder where the website files for a domain name are stored.
该文档根目录就是一个域名的网站文件存储的文件夹。
Document root directories might be named differently depending on your server configuration, however, they're conventionally named as such:
根据您的服务器配置,文档根目录可能会以不同的方式命名,但是,它们通常是这样命名的:
/www/
/public_html/
They also might live within another directory such as
它们也可能位于另一个目录中,例如
/home/myusername/public_html/
Your primary domain is generally rooted in the wwwor public_htmlfolder.
您的主域通常位于www或public_html文件夹中。
When someone tries to load your website and does not manually type in a file name (such as index.php or index.html), the server attempts to load the "directory index". The directory index is a listing of files it should load.
当有人尝试加载您的网站并且没有手动输入文件名(例如 index.php 或 index.html)时,服务器会尝试加载“目录索引”。目录索引是它应该加载的文件列表。
For example, if you type in domain.com (i.e: not domain.com/index.php), the server will attempt to load files in the following order. If a file is not found, then it tries to load the next file in the list:
例如,如果您输入 domain.com(即:不是 domain.com/index.php),服务器将尝试按以下顺序加载文件。如果未找到文件,则它会尝试加载列表中的下一个文件:
http://domain.com/index.html
http://domain.com/index.htm
http://domain.com/index.php
...and finally, if your server does not find any of these files, it then simply returns a listing of all of the files in the directory. See Image
...最后,如果您的服务器没有找到这些文件中的任何一个,它就会简单地返回目录中所有文件的列表。见图片
To prevent this, one should create an index.htmlfile and upload it to the server's document root directory as we mentioned before.
为了防止这种情况,我们应该创建一个index.html文件并将其上传到我们之前提到的服务器的文档根目录。
Try creating your first .html file with a simple text editor by copying the html markup below into a new file and then save it as index.html or download the file here
尝试使用简单的文本编辑器创建您的第一个 .html 文件,方法是将下面的 html 标记复制到一个新文件中,然后将其另存为 index.html 或在此处下载该文件
<html>
<head>
<title>My website's first HTML5 page</title>
</head>
<body>
<h1>Welcome!</h1>
<p>Sorry, but our website is under development.</p>
</body>
</html>
Proceed and upload the index.htmlfile to your server's document root directory, and then try to access your website domain. It will probably load your index.htmland your server will now display the contents of index.html file instead of the "DirectoryIndex listing of files".
继续将index.html文件上传到您服务器的文档根目录,然后尝试访问您的网站域。它可能会加载您的index.html,您的服务器现在将显示 index.html 文件的内容,而不是“DirectoryIndex 文件列表”。
WHEN IT DOES NOT WORK:
当它不起作用时:
If you've just placed an index.html or index.php file on your server's document root folder, for example: /public_html/ and you're still not getting it to load these files upon a request to your domain, chances are your server is missing a specific configuration for the "DirectoryIndex Directive".
如果您刚刚在服务器的文档根文件夹中放置了一个 index.html 或 index.php 文件,例如: /public_html/ 并且您仍然没有根据对您的域的请求来加载这些文件,则很可能是您的服务器缺少“DirectoryIndex 指令”的特定配置。
If that fails to happen, you can use the DirectoryIndex directive in an .htaccess file to specify a custom file or files that the web server looks for when a visitor requests a directory. To enable the DirectoryIndex directive, use a text editor to create/modify the .htaccess file as follows. Replace filename with the file that you want to display whenever a user requests the server's document root directory:
如果失败,您可以在 .htaccess 文件中使用 DirectoryIndex 指令来指定一个或多个自定义文件,当访问者请求目录时,Web 服务器会查找这些文件。要启用 DirectoryIndex 指令,请使用文本编辑器创建/修改 .htaccess 文件,如下所示。每当用户请求服务器的文档根目录时,将 filename 替换为您想要显示的文件:
DirectoryIndex filename
You can also specify multiple filenames, and the web server will search for each file until it finds a match.
您还可以指定多个文件名,Web 服务器将搜索每个文件,直到找到匹配项。
Open a text editor and copy the following example directive, then paste it on the new file:
打开文本编辑器并复制以下示例指令,然后将其粘贴到新文件中:
DirectoryIndex index.php index.html index.htm
Save the file as .htaccess
将文件另存为 .htaccess
No file extension, just name it as .htaccess placing a dot (.) in front of it to ensure it is a hidden file.
没有文件扩展名,只需将其命名为 .htaccess 并在其前面放置一个点 (.) 以确保它是一个隐藏文件。
In this directive, when a visitor requests the directory name, the web server looks first for an index.php file. If it does not find an index.php file, it looks for an index.html file, and so on until it finds a match or runs out of files to search.
在该指令中,当访问者请求目录名称时,Web 服务器首先查找 index.php 文件。如果它没有找到 index.php 文件,它会查找 index.html 文件,依此类推,直到找到匹配项或用完要搜索的文件。
Now upload the .htaccess file to your server's document root directory.
现在将 .htaccess 文件上传到您服务器的文档根目录。
It is recommended to restart the server in order to changes to take effect, although you could just try to refresh the directory to see if it works.
建议重新启动服务器以使更改生效,尽管您可以尝试刷新目录以查看它是否有效。
If you've got full control over your server you might find it easy restart/reload it. However, if you rely on a shared hosting service, maybe you'll not have enough permission to create an .htaccess file or to restart the server on your own, meaning that you would have to contact your hosting provider support team and request them to do that for you.
如果您完全控制了您的服务器,您可能会发现重新启动/重新加载它很容易。但是,如果您依赖共享托管服务,您可能没有足够的权限来创建 .htaccess 文件或自行重新启动服务器,这意味着您必须联系您的托管服务提供商支持团队并要求他们为你做那个。
I hope it helps you. Good luck.
我希望它能帮助你。祝你好运。
回答by Setu
Your question is highly confusing. Did you mean your website is opening index.html page on loading? or did you mean it is not loading index.html page. I will try to answer but you need to provide more information. If you type the link and index.html is opening than it is working correct because it will always open index file as homepage unless you have specified something else. secondly make sure you have uploaded all the files in correct folder because it is usual that when a user is inexperienced ftp could be confusing. So check it and add more details if possible.
你的问题非常令人困惑。您的意思是您的网站在加载时打开 index.html 页面吗?或者你的意思是它没有加载 index.html 页面。我会尽力回答,但您需要提供更多信息。如果您键入链接并且 index.html 正在打开,则它工作正常,因为除非您指定其他内容,否则它将始终将索引文件打开为主页。其次,确保您已将所有文件上传到正确的文件夹中,因为通常情况下,当用户没有经验时,ftp 可能会造成混淆。因此,请检查它并在可能的情况下添加更多详细信息。
回答by Programmer
The following could also be the reason for the same problem(In your case its not) as you mentioned and I am providing this for all New Learners who face such problems. if your website is hosted on IIS then you'll need to add the default Document. TO add this=>
以下也可能是您提到的相同问题的原因(在您的情况下不是),我为所有面临此类问题的新学习者提供此信息。如果您的网站托管在 IIS 上,那么您需要添加默认文档。添加这个=>
Double click on Default Documents icon in IIS, then if you don't see the index.html/php page then right click there and select Add and Type your index page name with extension and Click OK. Check your site in Browser.
双击 IIS 中的默认文档图标,然后如果您没有看到 index.html/php 页面,则右键单击该页面并选择添加并键入您的索引页面名称和扩展名,然后单击确定。在浏览器中检查您的网站。
回答by Jayanth sattineni
All you have to do is rename the home page as index.html that's it, it will automatically page stats running.
您所要做的就是将主页重命名为 index.html 就是这样,它会自动运行页面统计信息。
回答by kayleeFrye_onDeck
I felt so dumb when I ran into this issue the first time when trying to do something similar. Just run the index.html file directly, assuming everything was previously configured properly.
当我第一次尝试做类似的事情时遇到这个问题时,我感到很愚蠢。只需直接运行 index.html 文件,假设之前已正确配置所有内容。
You can either do that by directly using the file with say, Finder on Mac or File Explorer on Windows, or you can paste in the filesystem address right into the browser!
您可以直接使用文件,例如 Mac 上的 Finder 或 Windows 上的文件资源管理器,也可以将文件系统地址直接粘贴到浏览器中!
So if you're on say, Safari on a Mac, you'd plug this into your address bar:
因此,如果您使用 Mac 上的 Safari,请将其插入地址栏中:
file:///Users/username/repos/project_name/index.html
file:///Users/username/repos/project_name/index.html
And Bob's your uncle! ;)
而鲍勃是你的叔叔!;)