如何让 Apache 提供 index.php 而不是 index.html?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16192049/
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 Apache serve index.php instead of index.html?
提问by Adam Wojda
If I put the following line in a index.html
file, to make Apache include the index.php
file:
如果我将以下行放在index.html
文件中,以使 Apache 包含该index.php
文件:
<?php include("/Live/ls_client/index.php"); ?>
visiting the index.html
page shows me this:
访问index.html
该页面向我展示了这一点:
<!--?php include("/Live/ls_client/index.php"); ?-->
Why is that? Why doesn't it actually include the PHP file?
这是为什么?为什么它实际上不包含 PHP 文件?
回答by Glitch Desire
As others have noted, most likely you don't have .html
set up to handle php code.
正如其他人所指出的,您很可能没有.html
设置处理 php 代码。
Having said that, if all you're doing is using index.html
to include index.php
, your question should probably be 'how do I use index.php
as index document?
话虽如此,如果您所做的只是使用index.html
include index.php
,那么您的问题可能应该是“我如何将其index.php
用作索引文档?
In which case, for Apache (httpd.conf), search for DirectoryIndex
and replace the line with this (will only work if you have dir_module
enabled, but that's default on most installs):
在这种情况下,对于 Apache (httpd.conf),搜索DirectoryIndex
并替换该行(仅当您dir_module
启用时才有效,但这是大多数安装的默认设置):
DirectoryIndex index.php
If you use other directory indexes, list them in order of preference i.e.
如果您使用其他目录索引,请按优先顺序列出它们,即
DirectoryIndex index.php index.phtml index.html index.htm
回答by Dr Beco
As of today (2015, Aug., 1st), Apache2
in Debian Jessie
, you need to edit:
截至今天(2015 年 8 月 1 日),Apache2
在 中Debian Jessie
,您需要编辑:
root@host:/etc/apache2/mods-enabled$ vi dir.conf
And change the order of that line, bringing index.php to the first position:
并更改该行的顺序,将 index.php 带到第一个位置:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
回答by Shoe
PHP will work only on the .php
file extension.
PHP 将仅适用于.php
文件扩展名。
If you are on Apache you can also set, in your httpd.conf
file, the extensions for PHP. You'll have to find the line:
如果您使用的是 Apache,您还可以在您的httpd.conf
文件中设置PHP 的扩展名。你必须找到这一行:
AddType application/x-httpd-php .php .html
^^^^^
and add how many extensions, that should be read with the PHP interpreter, as you want.
并根据需要添加应使用 PHP 解释器读取的扩展数量。
回答by Bayron Jonathan Vazquez
You can't include php code in file with html extension you must change the extension of index.html for index.php and the other index file must have another name like template.php
您不能在带有 html 扩展名的文件中包含 php 代码,您必须将 index.html 的扩展名更改为 index.php 并且另一个索引文件必须具有另一个名称,例如 template.php
If a file have html extension the web server think that your php script is plain text.
如果文件具有 html 扩展名,则 Web 服务器认为您的 php 脚本是纯文本。