SOLVED PHP include in HTML File Question

val98

Member
May 21, 2018
6
2
3
PA
cPanel Access Level
Website Owner
This question is probably really dumb, but how do I get a html file to successfully call a php file?
I know my php file works because when I call it directly www.example.com/example.php it works, but when I put it in a html file using <? php include('example.php'); ?> it doesn't work. What am I doing wrong?
Thanks
 

24x7server

Well-Known Member
Apr 17, 2013
1,911
96
78
India
cPanel Access Level
Root Administrator
Twitter
Hi,

I know my php file works because when I call it directly www.example.com/example.php it works, but when I put it in a html file using <? php include('example.php'); ?> it doesn't work. What am I doing wrong?
A webserver has specific handler configured to execute specific files, like a PHP handler to use for executing .php file. PHP handler does display result of html too but the vice versa is not true. HTML coding is differnent, so when you have .html file, a webserver sees it like the same and does not execute PHP codes in it, as PHP handlers were not used for .html file to execute it.
 

dalem

Well-Known Member
PartnerNOC
Oct 24, 2003
2,977
155
368
SLC
cPanel Access Level
DataCenter Provider
public_html/.htaccess

easy way to achieve

rename the all files to php
then

add

RewriteRule ^([^.]+)\.html$ $1.php [L]

to your .htaccess

Now page.html will be a php page and php will run inside

optional we add this addition to our .htaccess

RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [L,R=301]


it just 301 redirects site.php pages to its html equivalent
 
  • Like
Reactions: cPanelMichael