Home >Backend Development >PHP Tutorial >How Can I Make Apache Treat HTML Files as PHP Files Using .htaccess?

How Can I Make Apache Treat HTML Files as PHP Files Using .htaccess?

Barbara Streisand
Barbara StreisandOriginal
2025-01-05 11:00:40412browse

How Can I Make Apache Treat HTML Files as PHP Files Using .htaccess?

How to Use .htaccess to Run HTML Pages as PHP Files

Need a quick solution to run all your HTML files as PHP before a presentation tomorrow? Don't fret; Apache has a solution for you.

(Answer)

To make all .html files run as .php files using Apache, follow these steps:

1. Create a .htaccess File

  • Navigate to the root directory of your website.
  • Create a new file named ".htaccess".

2. Add the Directive

  • Open the .htaccess file in a text editor.
  • Add the following line:
AddType application/x-httpd-php .html .htm

If you're running PHP as CGI, use this instead:

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

Example:

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

This directive tells Apache to treat all files with the extension .html or .htm as PHP files.

Note: If you're using PHP5, you may need to replace "x-httpd-php" with "x-httpd-php5" in the directive.

The above is the detailed content of How Can I Make Apache Treat HTML Files as PHP Files Using .htaccess?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn