1

I'm trying to test executing php code within my index.html. To test this I'm using:

<?php
        phpinfo();
?>

If I change the file to .php it works fine, but I want it to execute as .html . I set up a .htaccess to do this but it's not working. This is my .htaccess:

<Files index.html>
AddType application/x-httpd-php5 .html
</Files>

Both index.html and .htaccess are located in var/www/html. Why is the .html file not processing as .php when it has php code?

feners
  • 645
  • 5
  • 19
  • 48
  • I have a need to keep the .html extension. I'm not asking whether this is good practice or not. @anubhava – feners Feb 19 '18 at 19:35

1 Answers1

3

The reason why the web server is not passing the index.html file thru the PHP interpreter is that the mime-type has not been associated with it.

You need to use the AddHandler directive to set it.

In order to associate the type you've added, it really depends on what your setup is. Various scenarios might be: php-fpm, mod_php, cgi.

An example using CGI is:

Action application/x-httpd-php5 "/path/to/php"

Bear in mind that this setting is usually not available on shared hosting. If you are using one, consider contacting the support helpdesk.

Consider having a look at this duplicate question: Parsing HTML files as PHP

I would suggest against adding a handler. You should rather rename the index.html file to index.php. If this is not picked by Apache, you can correct it using DirectoryIndex index.php.

I hope this helps.

Nikola Petkanski
  • 4,724
  • 1
  • 33
  • 41