Search code examples
php.htaccessddev

Run php on .html pages with .htaccess


Wanting to use DDEV for local development, and I have a site with legacy content needing to run php on pages with .html extension. With other testing environments I have used different lines in .htaccess to enable this. With my ddev test environment I can't yet figure what works.

UPDATED Here are the lines I've tried (uncommenting singly or in combo) in .htaccess:

#AddType application/x-httpd-php .html .htm
#AddHandler application/x-httpd-php .htm .html

#AddType application/x-httpd-php7 .html .htm
#AddHandler application/x-httpd-php7 .htm .html
    
#AddHandler x-httpd-php .htm .html
#AddType x-httpd-php .html .htm
    
#AddType x-httpd-php7 .html .htm
#AddHandler x-httpd-php7 .htm .html

#AddType x-httpd-php73 .htm .html
#AddHandler x-httpd-php73.htm .html
#AddHandler x-httpd-php7-3 .htm .html
      
#AddType application/x-httpd-php73 .html .htm
#AddType application/x-httpd-php7-3 .html .htm
#AddHandler application/x-httpd-php7-3 .htm .html

I also tried updating the .ddev/apache/apache-site.conf with something like:

<FilesMatch ".html$"> 
SetHandler "proxy:fcgi://php:9000" 
</FilesMatch> 

or

<FilesMatch ".+\.html$"> 
SetHandler applicaiton/x-httpd-php 
</FilesMatch> 

but admittedly this is not something I'm familiar with so probably not getting those quite right.

macOS Big Sur, ddev1.16.5 with php7.3, ddev webserver_type: apache-fpm.

Any suggestions? Thanks!


Solution

  • The problem is that php-fpm is not configured to allow use of the .html file extension. This is configured in /etc/php/<version>/fpm/pool.d/www.conf.

    So you have two things to do to enable the behavior you're after, allowing PHP interpretation of HTML files.

    1. Allow processing html files in the php-fpm configuration, in pool.d/www.conf. You can do this using the answer in https://stackoverflow.com/a/65693405/215713
    2. Allow processing html files in the apache configuration. I'm sure there is more than one way to do it, and you may be able to do it in .htaccess, but I did it by adding this stanza the port 80 virtualhost in .ddev/apache/apache-site.conf (and of course removing the #ddev-generated at the top of the file):
     <FilesMatch ".+\.(html|ph(ar|p|tml))$">
          SetHandler "proxy:unix:/var/run/php-fpm.sock|fcgi://localhost"
      </FilesMatch>