问题描述
我已经为本地计算机设置了虚拟主机.
I've set up a virtual host for my local machine.
这是我的/etc/hosts
文件中的内容:
This is what I have in my /etc/hosts
file:
127.0.0.1 localhost local.dev
127.0.1.1 tomica-ubuntu
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
这是我的/opt/lampp/etc/extra/httpd-vhosts.conf
中该虚拟主机的配置:
This is configuration for that virtual host in my /opt/lampp/etc/extra/httpd-vhosts.conf
:
<VirtualHost *:80>
DocumentRoot "/opt/lampp/htdocs/dev"
ServerName local.dev
</VirtualHost>
在我的/opt/lampp/htdocs/dev/index.html
中,我有这个:
<html>
<body>
<p>HTML</p>
<?php echo 'PHP' ?>
</body>
</html>
但是当我在浏览器中打开http://local.dev
时,我只会看到:
But when I open http://local.dev
in my browser, I only see:
HTML
但是,如果我打开文档源,可以看到:
However, if I open the document source, I can see:
<html>
<body>
<p>HTML</p>
<?php echo 'PHP' ?>
</body>
</html>
如果我检查页面的DOM,就会发现:
And if I inspect the page's DOM, there's:
<html>
<body>
<p>HTML</p>
<!--<?php echo 'PHP' ?>-->
</body>
</html>
此外,如果我将/opt/lampp/htdocs/dev/index.html
重命名为/opt/lampp/htdocs/dev/index.php
,一切似乎都很好.
Also, if I rename /opt/lampp/htdocs/dev/index.html
to /opt/lampp/htdocs/dev/index.php
everything seems to be alright.
为什么我的PHP代码没有在.html文档中解析?
Why is my PHP code not parsing in the .html document?
推荐答案
默认情况下,mod_php
不会告诉Apache让它处理带有.html
扩展名的文档.如果您使用 AddHandler
指令更改处理程序类型:
By default mod_php
does not tell Apache to let it handle documents with the .html
extension. If you change the handler type using the AddHandler
directive:
AddHandler php-script .html
然后Apache将知道您想让PHP处理扩展名为html
的文件的内容.
then Apache will know that you want to let PHP process the contents of files with a html
extension.
默认情况下未启用此功能的原因是,通过PHP解释器运行文档需要花费(CPU,内存使用量,最终用户时间).浪费时间是没有意义的-因此在mod_php的默认设置HTML文件(通常是静态事务)中不会通过PHP解释器传递.
The reason this is not enabled by default is because running a document through the PHP interpreter costs (in CPU, in memory usage, in end-user time). There is no point in wasting time - so in mod_php's default setup HTML files (which normally are static affairs) are not passed through the PHP interpreter.
这篇关于XAMPP-PHP无法在virtualhost上解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!