This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I add PHP code to .html files?
is there a way to process php code on a .html/.htm file? the server supports php I just need the file to retain the .htm extension
Note: I really don't understand why some users feel the need to down vote on a valid question. Not all questions appear in the search unless it worded close to the previous question.
Thanks for all the answers below. AddType in the htaccess solved my problem
Add the following to your .htaccess:
AddHandler application/x-httpd-php5 .htm
You can either use mod_rewrite, or add a directive to your .htaccess file to tell Apache (if that's what you're using) to add .htm as a PHP file type.
You can change the file config of your web server (for example, in apache the httpd.conf file) to interprete the .htm as it was a php file.
If you use Apache, look for the php addHandler directive in the apache2.conf file and add your extension in the list. Do not forget to restart apache after.
Related
This question already has answers here:
Parsing HTML files as PHP
(3 answers)
Closed 5 years ago.
I'm trying to test executing php code within my index.html. To test this I'm using:
<?php
phpinfo();
?>
If I change the file to .php it works fine, but I want it to execute as .html . I set up a .htaccess to do this but it's not working. This is my .htaccess:
<Files index.html>
AddType application/x-httpd-php5 .html
</Files>
Both index.html and .htaccess are located in var/www/html. Why is the .html file not processing as .php when it has php code?
The reason why the web server is not passing the index.html file thru the PHP interpreter is that the mime-type has not been associated with it.
You need to use the AddHandler directive to set it.
In order to associate the type you've added, it really depends on what your setup is. Various scenarios might be: php-fpm, mod_php, cgi.
An example using CGI is:
Action application/x-httpd-php5 "/path/to/php"
Bear in mind that this setting is usually not available on shared hosting. If you are using one, consider contacting the support helpdesk.
Consider having a look at this duplicate question: Parsing HTML files as PHP
I would suggest against adding a handler. You should rather rename the index.html file to index.php. If this is not picked by Apache, you can correct it using DirectoryIndex index.php.
I hope this helps.
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 6 years ago.
I have been trying to setup an environment to develop php and html on my windows 10 machine.
I've spent all evening and finally I believe I have Apache 2.4 and php 7 installed correctly as I can correctly execute info.php () through the localhost:667/info.php.
However, any php inside my html docs located in localhost:667 (apache24/htdocs) does not work.
Here are the lines I added to apache/httpd.conf:
LoadModule php7_module "c:/php/php7apache2_4.dll"
AddType application/x-httpd-php .php
AddHandler application/x-httpd-php .php
DirectoryIndex index.html index.php
#configure the path to php.ini
PHPIniDir "C:/php"
...as well as redefining the listening port as default 80 was conflicting with something.
I renamed php/php.ini-development to php.ini as instructed by an installation manual, and have uncommented the line:
extension_dir = "ext"
as per installation instructions.
Why is the php in my html files still not executing and being shown raw in the source output?
Thanks in advance and let me know if you need more information.
Ok so Apache 2.4 httpd.conf isn't setup to parse php embedded in .htm/.html extensions by default.
Adding the line
AddHandler php7-script .php .html .htm
...to the file fixed this issue.
Sigh. Seems obvious now I see it. At least I have learned something. Seems documentation provided by php to install their product isn't exactly extensive. I get the feeling php is a little neglected.
I am behind on the times, are people using another other methods now?
Thanks for all participants.
This question already has answers here:
Process HTML files like PHP
(5 answers)
Closed 7 years ago.
I am totally new to php coding and my teacher told us that PHP files should be renamed with .php extension for it to work. But I have files in the serve with .htm extension, which I used with Google Analytics and Search Console, so now I don't want to change the extension to all my 5 five pages as I would make a huge chaos out of it. So is there a way I can insert php code into my html pages and still use it with .htm extension?
Thank you!
You want to write PHP codes within the .HTM / .HTML files
all you have to do is the add the following lines to your httpd.conf
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
note: restart your apache or rehash it so that the configurations take effect
Yes, you can use php code inside .html files, But you need to config the websever. In Apache you can config like this:
# Interpret both .php & .html as PHP:
AddHandler php5-script .php .html
As reference look this answer here
Hi you cannot run PHP withouth .php extension files. But you can work with friendly-urls.
You can do something like that
In your .htaccess file
RewriteEngine on
RewriteRule ^/example/([0-9]+)\ /example.php?id=$1
And this maps requests from
/example.php?id=contact
to
/example/contact
You can find more about it in Google typing "How I can work with url firendly in php".
Short answer: No.
Explained: The .php tells the server that the file has php information and needs to run the select code as php. Otherwise it'll run as normal html and won't run your code.
You can link to a separate .php page when something requires the php code. Yet if it's for a page with dynamic loading then you just have to change the extension.
As we know, it is easy to get Apache to handle .html pages as PHP pages by adding the following line to http.conf:
AddHandler application/x-httpd-php .php .html
How can this be done in OpenShift?
How can I edit http.conf in OpenShift?
Or is there another way?
Have you tried using a .htaccess file? Try the answer from this stackoverflow question, but use php instead of perl/python: perl on php application on openshift
It's easy, just add the following line to your .htaccess in any folders that it is required: AddHandler application/x-httpd-php .php .html
I'd like to accept other type of files that contains PHP code. For example, it would be nice to read an .aspx file by PHP as if it were .php.
Add this to your .htaccess file in Apache to make html parse as PHP:
AddType application/x-httpd-php .html
You can extrapolate what you need to do from there. :-)
Use this htaccess rule:
AddType application/x-httpd-php .php .aspx
Yes, this is done in your Apache configuration. You tell apache what kind of files you want it to send to the PHP engine. You can do this configuration the same way you do other apache configuration - in the main config file, per site, or in .htaccess files.