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.
- 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
- 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
Using Jquery $.Ajax to Call a PHP Function
PHP - Concatenate or Directly Insert Variables in String
PHP Function to Generate V4 Uuid
Do Htmlspecialchars and MySQL_Real_Escape_String Keep My PHP Code Safe from Injection
Best Way to Insert Many Values in MySQLi
How to Sort Files by Date in PHP
Generating Random Results by Weight in PHP
Undefined Function MySQL_Connect()
How to Change From-address When Using Gmail Smtp Server
Fatal Error: Call to a Member Function Fetch_Assoc() on a Non-Object
Convert PHP Array to JavaScript
Creating Default Object from Empty Value in PHP
How to Find All Youtube Video Ids in a String Using a Regex
Compare 2-Dimensional Data Sets Based on a Specified Second Level Value