I have PHP scripts inside my HTMl file. I am using Apache. I created a .htaccess file and place it in my root directory of the application with the following as its contents

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

I want to allow the server to execute the PHP scripts inside the HTML files.

Thanks in advance.

Dani AI

Generated

A short, practical summary and safer options based on the thread

Several replies here correctly point out the simplest option is to use the .php extension () and the OP () confirmed a misplaced .htaccess was the real problem. When renaming is not possible, it is better to map only the files that actually need PHP to a PHP handler (instead of globally forcing every .html/.htm). This avoids surprising behavior and reduces risk.

Safer handler mapping (recommended)
Use a handler mapping that targets the exact extensions or files needed, for example with a FilesMatch + SetHandler block rather than relying on AddType. SetHandler binds a request to the PHP handler directly; AddType is for MIME types and is the older/less-precise approach. A common pattern is:

<FilesMatch "\.(html|htm)$">
  SetHandler application/x-httpd-php
</FilesMatch>

Adjust the handler name if the host exposes a versioned handler (e.g. application/x-httpd-php7.4). (php.net)

Troubleshooting checklist

  • If directives in .htaccess seem ignored, verify the site’s Apache config allows .htaccess overrides (AllowOverride must permit the directives used).
  • Confirm which handler the server expects (a quick test can print the handler value via PHP).
  • Check Apache error log after placing .htaccess to catch syntax or permission problems. (httpd.apache.org)

Performance and security notes
Parsing every .html through PHP adds request cost and can expose mistakes (e.g., inadvertently executing uploaded files with deceptive names). For low-traffic sites the cost is small, but on busier sites it matters — prefer scoping the change to specific files or a subdirectory, or use URL rewriting to present “pretty” URLs while keeping .php files where sensible. (stackoverflow.com)

Relevant forum contributors: , , , , , — their suggestions (rename where possible; check .htaccess placement and server permissions) align with the safer patterns above.

Recommended Answers

All 10 Replies

Member Avatar for Member #120589

Why not just rename the extensions to .php?

This means the server has to process all html files, whether they contain php or not. You can mod_rewrite urls if you don't want to see a file extension at all.

if you're in windows, you can do it

yea... am using windows.. I wanted to rename the extension to PHP but my supervisor didn't wanted it like that..

Thanks for your responses!

Member Avatar for Member #120589

Get your supervisor to explain to you why his way is better.

Member Avatar for Member #46692

if you're in windows, you can do it

Or any other operating system for that matter.

Member Avatar for Member #120589

if you're in windows, you can do it

Waste of time noob post - best ignored.

Ditto Diafol above
get the supervisor to explain why they want to break a funtional system

Hi,

As per my understanding you need all files are in .htm or .html but work like .php files.
i.e. aboutus.htm will work like aboutus.php, am I correct?

Please let me know.
Thanks,
Ajay

I had a talk with my supervisor and he said that he wanted to see if I would be able to figure it out. I got it to work. It turns out that I did not place the .htaccess file in the root directory.

But he told me that I can use the php extension for the application...

Thanks for your responses.

just rename the extension .php

commented: read the posts before repeating -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.