I need to run all of my .html files as .php files and I don't have time to change all of the links before our presentation tomorrow. Is there any way to "hack" this with my Apache server?
-
2Though it's not a direct answer to your question, using `sed` or any good IDE will allow you find-and-replace across an entire directory/project. Also, you can batch rename files pretty easily on Linux. – coreyward Jan 14 '11 at 01:43
-
Yes but if you rename the files using sed, you might get many broken links. Of course, you can also try to change all the links in your website, but there's also links from all over the Internet that might point to your website. – Marc-François Jan 14 '11 at 02:26
19 Answers
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

- 4,984
- 7
- 54
- 116

- 3,900
- 3
- 28
- 47
-
8I had to add `5` to the end of the type for it to work, i.e. `AddType application/x-httpd-php5 .html .htm` (CentOS running PHP 5.3) – melat0nin Apr 09 '14 at 09:17
-
23This doesn't work for me. It makes Firefox want to download the html file and display it. – Lee Blake Apr 17 '14 at 00:30
-
2
-
@melat0nin, Is `application/x-httpd-php5` hard coded? Could it have been something else? – Pacerier Aug 11 '15 at 07:48
-
Just want to add that `AddHandler application/x-httpd-php .html .htm` should also be used if you are running PHP as FCGId – Maghoumi Apr 13 '16 at 04:24
-
@LeeBlake It is important to enable the module `rewrite` and resolve `AllowOverride All` – Andrei Krasutski Sep 01 '17 at 18:01
-
4
-
1
In My Godaddy Server the following code worked
Options +ExecCGI
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php5 .php .html

- 39,603
- 20
- 94
- 123

- 139
- 1
- 3
This is in edition to all other right answers:
If you are not able to find the correct Handler, Simply create a .php file with the following contents:
<?php echo $_SERVER['REDIRECT_HANDLER']; ?>
and run/open this file in browser.
Use this output in .htaccess file
Create a .htaccess file at the root of your website(usually a folder named public_html or htdocs on linux servers) and add this line:
AddType [[THE OUTPUT FROM ABOVE FILE]] .html .htm
Example
AddType application/x-httpd-php70 .html .htm
Important Note:
If you see blank page or Notice: Undefined index: REDIRECT_HANDLER
Try default in .htaccess
AddHandler application/x-httpd-php .html
-
1
-
2Is there a way to determine this handler in another way? Because $_SERVER['REDIRECT_HANDLER'] is returning null in my case :( – Fox Jun 11 '20 at 07:01
You may also use the H or T flag of mod_rewrite to force all .html files to be parsed by php handler :
using H (Handler) flag:
RewriteEngine on
RewriteRule \.(html|htm)$ - [H=application/x-httpd-php5]
using T (Type) flag :
RewriteEngine on
RewriteRule \.(html|htm)$ - [T=application/x-httpd-php5]
Or you can add more extensions to the rule pattern seprated by a pipe | that you want to be parsed by php handler
ex :
RewriteRule \.(html|htm|txt|foo)$ - [T=application/x-httpd-php5]
the example above will change the mime type of files that end with .html , .htm , .txt , .foo to php.
Note : on some servers you will have to change php5 to php to get this example to work in handler string:
Change it
[T=application/x-httpd-php5]
to
[T=application/x-httpd-php]

- 40,709
- 21
- 93
- 115
You need to add the following line into your Apache config file:
AddType application/x-httpd-php .htm .html
You also need two other things:
Allow Overridding
In
your_site.conf
file (e.g. under/etc/apache2/mods-available
in my case), add the following lines:<Directory "<path_to_your_html_dir(in my case: /var/www/html)>"> AllowOverride All </Directory>
Enable Rewrite Mod
Run this command on your machine:
sudo a2enmod rewrite
After any of those steps, you should restart apache:
sudo service apache2 restart
Normally you should add:
Options +ExecCGI
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php5 .php .html
However for GoDaddy shared hosting (php-cgi), you need to add also these lines:
AddHandler fcgid-script .html
FCGIWrapper /usr/local/cpanel/cgi-sys/php5 .html

- 155,785
- 88
- 678
- 743
-
-
I am using godaddy and that is the only one that's worked for me. others either don't work or download the html page without displaying it! – Mazen Elkashef Aug 03 '16 at 17:21
Running .html files as php stopped working all of a sudden in my .htaccess file.
Godaddy support had me change it to:
AddHandler application/x-httpd-lsphp .html

- 79
- 6
-
1**The correct answer varies** depending on webserver configuration. This one is the correct answer in my case too (and I'm *not* with GoDaddy) ...so if you're having problems, it's probably best to check with your web host. – ashleedawg Dec 23 '18 at 15:02
-
It depends wether your hosting provider uses LSAPI (LiteSpeed) version of PHP handler then you have to use `application/x-httpd-lsphp` instead of `application/x-httpd-php` – sbrbot Jun 04 '19 at 10:13
-
Using @Marc-François approach Firefox prompted me to download the html file
Finally the following is working for me (using both):
AddType application/x-httpd-php .htm .html
AddHandler x-httpd-php .htm .html

- 954
- 1
- 11
- 22
AddHandler application/x-httpd-php .php .html .htm
// or
AddType application/x-httpd-php .php .htm .html

- 11
- 2
-
2Please use the [edit] link to explain how this code works and don't just give the code, as an explanation is more likely to help future readers. See also [answer]. [source](http://stackoverflow.com/users/5244995) – Jed Fox Jan 27 '17 at 22:50
I'm using PHP7.1 running in my Raspberry Pi 3.
In the file /etc/apache2/mods-enabled/php7.1.conf
I added at the end:
AddType application/x-httpd-php .html .htm .png .jpg .gif

- 140
- 1
- 9
On Dreamhost Servers you can refer to this page that at time of writing indicates you may use the following for php 7.2 with FastCGI:
AddHandler fcgid-script .html
FcgidWrapper "/dh/cgi-system/php72.cgi" .html
Or if you are using php5 cgi (not FastCGI):
AddHandler php5-cgi .html

- 5,797
- 4
- 27
- 36
None of the answers posted here worked for me.
In my case the problem was, by the one hand, that the .conf file (/etc/apache2/sites-available/default-ssl.conf or /etc/apache2/sites-available/000-default.conf) did not contain the directive AllowOverride All
for the site directory, which caused the .htaccess to not been processed. To solve this, add:
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
On the other hand, the problem was that the .htaccess was created by the root user, and therefore the apache user could not read it. So, changing the file owner solved definitely the problem:
chown www-data:www-data .htaccess

- 893
- 1
- 10
- 25
First, read this: https://httpd.apache.org/docs/current/howto/htaccess.html#when
Then read my post here: https://stackoverflow.com/a/59868481/10664600
sudo vim /etc/httpd/conf/httpd.conf

- 1,680
- 1
- 12
- 26
-
An answer should contain.. well the answer, and not just link somewhere where it migth be written :) – Markus Jan 22 '20 at 22:31
-
If you copy and paste the answer to a question everywhere that someone asked the same question, the moderators here will delete some of them and tell you to post links to the best one. – Adam Winter Jan 23 '20 at 21:28
Sometimes it doesn't work, if you just add the .htaccess file to the directory. In my case, I also changed an entry in the apache2 configuration.
sudo nano /etc/apache2/apache2.conf
...
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Modification:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
After that you only must restart the apache2-service:
sudo systemctl restart apache2
This worked for me.

- 13
- 2
This would help anyone,
RewriteEngine on
RewriteRule ^(.+)\.html$ $1.php [L]
Thanks @FlamberHansen.

- 1,690
- 3
- 21
- 39
If you're on Plesk running PHP as an FPM application served by Apache this article worked for me:
Go to Domains > example.com > PHP Settings and add the following lines into the field Additional configuration directives:
[php-fpm-pool-settings]
security.limit_extensions = .php .phar .html .inc
.htaccess
<Files ~ .(?i:inc|html|htm)$>
SetHandler proxy:unix:///var/www/vhosts/system/EXAMPLE.com/php-fpm.sock|fcgi://127.0.0.1:9000
</Files>

- 8,402
- 3
- 34
- 72