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

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?

This question is related to php html apache .htaccess

The answer is


For anyone out there still having trouble,

try this (my hosting was from Godaddy and this is the only thing that worked for me among all the answers out there.

AddHandler x-httpd-php5-cgi .html

here put this in your .htaccess

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

more info on this page


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


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

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

I think this is the best way to run php script on html and htm pages:

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

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

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]

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.

Output from the php code, copy this output

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

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

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

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

Source: Parse HTML As PHP Using HTACCESS File On Godaddy.


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

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:

  1. 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>
    
  2. 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
    

In My Godaddy Server the following code worked

Options +ExecCGI
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php5 .php .html

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to apache

Enable PHP Apache2 Switch php versions on commandline ubuntu 16.04 Laravel: PDOException: could not find driver How to deploy a React App on Apache web server Apache POI error loading XSSFWorkbook class How to enable directory listing in apache web server Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details How to enable php7 module in apache? java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing while starting Apache server on my computer

Examples related to .htaccess

Use .htaccess to redirect HTTP to HTTPs Getting a 500 Internal Server Error on Laravel 5+ Ubuntu 14.04 Server unable to read htaccess file, denying access to be safe Laravel 5 – Remove Public from URL Laravel 5 not finding css files How can I fix the 'Missing Cross-Origin Resource Sharing (CORS) Response Header' webfont issue? How Can I Remove “public/index.php” in the URL Generated Laravel? Apache 2.4 - Request exceeded the limit of 10 internal redirects due to probable configuration error Forbidden You don't have permission to access / on this server Htaccess: add/remove trailing slash from URL