Parse PHP with .html extension in Apache - best practice?

Hello!

I’ve searched the SitePoint archives and found a few topics, but all are at least 4 years old.

I have a site where I’d like to continue using the (well established) .html file extensions, but also use php includes for shared elements. I’m very new to PHP and I was wondering whether there’s an accepted best practice for having Apache parse PHP within HTML documents.

http://marcellinosantoso.com/how-to-call-php-script-within-html-files/ (edit: warning…this site looks like it’s having a twitter API problem and is prompting you for a password)

Googling around, I’ve found you can either edit the .htaccess file or use the Apache Handlers page in cPanel.

Are either of these preferred? Are there any downsides to either?

Thank you!

They are the same method, the control panel is just doing the file editing for you. In either case, you’re adding an AddHandler directive to your web server configuration telling it to pass .htm/.html files to the PHP interpreter.

Thanks for the reply, Dan

Is this a common practice? Will I take a big performance hint this way (significantly more than if the files had a .php extension)?

None at all, this is how Apache is told to parse .php files as PHP as well

The actual file extension is completely irrelevant

excellent. that saves me a lot of potential headache. thanks, dan!

Parsing .html files as PHP does not incur a performance penalty vs. parsing .php files as PHP per se. However, the server will parse all HTML documents as PHP, whether they contain PHP code or not. That may or may not cause unacceptable overhead.

Considering that using a PHP CMS or creating a new PHP website is as common, or more common, than writing HTML files these days, it’s hard to think of a situation where that would cause unacceptable overhead. It wouldn’t with any normal sized website on any standard web host at least.

It’s hard to imagine such a situation, but hard-to-imagine situations tend to arise with frightening frequency when we dismiss them as improbable. It’s like some perverse axiom of Murphy’s Law.

doing it through cpanel as described in that link doesn’t seem to work

i set .html and .htm files server-parsed, but <?php ?> stuff shows up as unparsed.

any ideas?

Don’t confuse “server-parsed HTML”, i.e., Server Side Includes (SSI) with PHP parsing. Ultimately, Apache must be aware that it is supposed to use PHP to parse the files. If CPanel doesn’t issue the proper directives to Apache, then it won’t work.

Dont take offence, but you need to stop and start apache to get that working, IIRC.

no offense taken! appreciated, I’ll try it out

I advise against parsing .html files as php, because you open a new territory for many mistakes, you need to remember to check all 3rd party files that have .html extension when uploading to the site in the future (to be sure they don’t contain any malicious code), make sure that any modules that allow uploading attachments, files, do not allow uploading .html files.

Instead you could go with url rewriting, see mod_rewrite in Apache for example. Url may look like: example.com/page.html but the script being executed for that url will be example.com/page.php.

For example put this in .htaccess:

RewriteEngine On
RewriteRule ^page\.html$ /page.php [L]

interesting. this looks like the preferred method. thanks for the reply