How to run PHP with .htm or .html extension? [duplicate] - php

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.

Related

Using .htaccess to run PHP code within index.html [duplicate]

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.

what code do i put in .htacess to use php?

Just wanted to know what code I have to put in my .htaccess file so my html pages read php that im going to put in there, (just a contact form), I did find it once but can't remember, sorry and thanks.
You have to convert your html files to php
put this line in your .htaccess if you're using apache2
AddType application/x-httpd-php .html .htm
but make sure that your .htaccess file is in the root directory of the website
If you want PHP in HTML files to be interpreted just add this line to your .htaccess file:
AddType application/x-httpd-php .html .htm
HTML Pages don't read your PHP. Instead Apache is reading your file and executes it with a PHP interpreter if some PHP is in there.
Just in case you have a debian server with apache2 you can apt-get install php5. But there is a lot more work to do, for example FCGI and so on.

.html vs .php | Difference, when I write php in both extension types

I know that we can write PHP in .html format if we edit the .htaccess file.
ddType application/x-httpd-php .htm .html
What is the difference between editing .htaccess and write PHP codes in .html file and writing PHP codes in .php file?
There is no difference. PHP written in .html works exactly like PHP written in .php files. However, you cannot write HTML in .php files.

PHP in HTML files not running or being commented out

First of all, I am not trying to run php within a js script, there is a similar question on here that refers to a user trying to run php from inside a js script.
I have added many combinations of
AddType application/x-httpd-php .php .html
AddHandler x-httpd-php .html
to the .htaccess file in a higher level directory containing the .html file I want to run php in. This has not worked. (I am open to trying new combinations)
The is either not read at all or commented out when viewing the source in broswer.
My question is how to get to be run inside of an html file OR is there a better way to include php functionality in an html document without having the code in the same document.
Additionally my host uses cpanel if this helps anything.
I can elaborate on anything I need to, thanks in advance.
You can try to use mod_rewrite for that task:
RewriteEngine On
RewriteRule ^(.*).html index.php [QSA]

processing PHP code on a .htm extension file? [duplicate]

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.

Categories