Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save a PHP file with a .php or .html extension?

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.

like image 962
parmanand Avatar asked Nov 29 '22 18:11

parmanand


2 Answers

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.

like image 189
Spudley Avatar answered Dec 04 '22 11:12

Spudley


You can do it, but you have to modify your .htaccess file:

RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
like image 43
KJYe.Name Avatar answered Dec 04 '22 10:12

KJYe.Name