问题
I've set up a virtual host for my local machine.
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
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>
In my /opt/lampp/htdocs/dev/index.html
I have this:
<html>
<body>
<p>HTML</p>
<?php echo 'PHP' ?>
</body>
</html>
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>
And if I inspect the page's DOM, there's:
<html>
<body>
<p>HTML</p>
<!--<?php echo 'PHP' ?>-->
</body>
</html>
Also, if I rename /opt/lampp/htdocs/dev/index.html
to /opt/lampp/htdocs/dev/index.php
everything seems to be alright.
Why is my PHP code not parsing in the .html document?
回答1:
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
then Apache will know that you want to let PHP process the contents of files with a html
extension.
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.
来源:https://stackoverflow.com/questions/12541345/xampp-php-not-parsing-on-virtualhost