Why do we have a PHP file as .php? Can we save a PHP file as .html, because at some sites, I have seen webpages with the .php extension? Why can't we have a .html extension for a PHP file?
Does it effect the execution of the PHP file on the PHP server engine? If we have it as a .html file, which has PHP code inside it.
If you really want to serve your PHP code with .html files, it can be done.
Your web server is configured to detect the file type by looking at the extension. By default it will route .php files through the PHP interpreter, and .html files will be served directly to the end user. You can configure this behaviour via the server's config file, or the local .htaccess file for the individual web directory. See @kjy112's answer for how to do this.
The question is why? Is it important for you to do this, or is it just something you want as a nicety. Ask yourself: Will your end-user care? or even notice?
The reason the browser is configured this way by default is so that plain HTML files that don't have any PHP code can be served to the user without going through the overhead of being processed by the PHP interpreter. By changing the configuration, all your HTML files will be interpreted as PHP even if they don't actually contain any PHP code, so you'll be adding extra processing load to your server. Of course, if all your files have some element of PHP code, you're going to want this behaviour anyway, so this obviously wouldn't worry you.
So it can be done. But there may be a better solution for you:
The current trend for SEO (search engine optimisation) is to get rid of file extensions in the URL entirely. If you look at the URL for this question, you'll see that it has a very descriptive URL, and without an extension. If you want to follow current trends of best-practice, you may want to consider going down this route instead.
This is done using mod_rewrite
. The descriptive part of the URL is completely arbitrary: if you change it, the page will still load correctly. The important part is the ID number before the description; the description is simply there to give the page a boost with it's Google ranking. A full discussion of the techniques involved is outside the scope of this question, but there are plenty of resources around that will help you (try searching for mod_rewrite right here on this site to start with).
Note: All the specific server config advice I've given here is applicable to the Apache web server. If you're using IIS or some other server product, you may need to adapt it accordingly.
You can do it, but you have to modify your .htaccess file:
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With