1

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?

Томица Кораћ
  • 2,542
  • 7
  • 35
  • 57

1 Answers1

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.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • Wow, that was fast. Thank you @Sean. Although I had to modify it a little bit. So in `/opt/lampp/htdocs/dev` I created a `.htaccess` file and put this in: `AddHandler php5-script .html` – Томица Кораћ Sep 22 '12 at 06:50
  • @ТомицаКораћ - that will work too - I was thinking that it could be added to your `VirtualHost` section in your `.conf` file - but .htaccess will work too :-) – Sean Vieira Sep 22 '12 at 06:53
  • I got it, it's just that I feel I'm still too new in server setting that I'm trying to do as much as possible on a per-project basis :) Your help is much appreciated. – Томица Кораћ Sep 22 '12 at 06:59