Using .Htaccess to Make All .Html Pages to Run as .PHP Files

Using .htaccess to make all .html pages to run as .php files?

Create a .htaccess file at the root of your website and add this line:

[Apache2 @ Ubuntu/Debian: use this directive]

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

Or, from comment below:

AddType application/x-httpd-php5 .html .htm

If your are running PHP as CGI (probably not the case), you should write instead:

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

Using .htaccess to run PHP code within index.html

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.

.htaccess Redirect all html files to index.php

Add this rule

RewriteRule ^(.*)$ /index.php?$1 [L]

Run php on .html pages with .htaccess

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>


Related Topics



Leave a reply



Submit