Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must a webpage end in ".php" in order to run php code?

Tags:

html

php

Does a webpage need to end in ".php" in order to run php code? Is there a way a file ending in ".html" can run php code?

like image 240
Yarin Avatar asked Dec 09 '10 14:12

Yarin


People also ask

What is required to run PHP?

To run PHP for the web, you need to install a Web Server like Apache and you also need a database server like MySQL. There are various web servers for running PHP programs like WAMP & XAMPP. WAMP server is supported in windows and XAMP is supported in both Windows and Linux.

How do I run a PHP script from a website?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.

What does .PHP mean at the end of website?

PHP (recursive acronym for PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

Why is my PHP code not working in HTML?

Php files can always read and display HTML code, but HTML does not automatically parse php code. To do so, you will need to make adjustments to your . htaccess file. Once that is done, the php code will display within HTML files without issue.


2 Answers

If you're using the Apache web server you can add this line to its configuration:

AddType application/x-httpd-php .html

it tells the the server that files with a .html extension have to be considered as PHP files. I don't recommend this though, as it forces the PHP engine on every file (even static html pages).

Alternatively you can rewrite (some) .html URLs to their PHP version via mod_rewrite.

like image 112
Matteo Riva Avatar answered Oct 05 '22 23:10

Matteo Riva


No a webpage doesn't need to end in .php to be parsed as PHP.

Yes you can use any extension you want depending on your web server, for example the below code placed in the Apache configuration would parse files with a .htm, .html or .php extension as PHP if running on an Apache server.

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

And you may also try this with regular expressions to better match specific criteria:

<FilesMatch "\.(htm|html|php)$">
    SetHandler application/x-httpd-php
</FilesMatch>
like image 26
David Hancock Avatar answered Oct 05 '22 23:10

David Hancock