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

36,548

Solution 1

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.

Solution 2

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

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

Solution 3

Rewrite Info here

all requests to file.htm will be sent to file.php:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [NC]

Solution 4

An alternative to kjy's answer is to use URL rewriting, it will let you use whatever page extensions/file names you want.

Share:
36,548
parmanand
Author by

parmanand

I'm Just a Curious man,who likes stack-exchange sites,because they really responds.

Updated on December 01, 2020

Comments

  • parmanand
    parmanand over 3 years

    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.

  • parmanand
    parmanand over 13 years
    if ,we save file as .html, and set rules what u said for .htaccess file, does this rule imposed on every file on server or it will be focused around some specific directory,which are needed to be served.
  • parmanand
    parmanand over 13 years
    whats better ,modifying .htaccess file or url rewriting according to u.
  • parmanand
    parmanand over 13 years
    whats better ,modifying .htaccess file or url rewriting according to u.
  • parmanand
    parmanand over 13 years
    whats better ,modifying .htaccess file or url rewriting according to u.
  • KJYe.Name
    KJYe.Name over 13 years
    @DemlaPawan .htaccess files provide a way to make configuration changes on a per-directory basis. httpd.apache.org/docs/current/howto/htaccess.html and if you do have access to main server configuration you can apply it to that if you want it to effect entire server, but generally speaking its not a good practice applying to entire server
  • Tom Gullen
    Tom Gullen over 13 years
    @Demla Rewrite urls, it's basically the standard. It will be more portable as well if you ever move server, and you don't need to worry about security risks of having all HTML pages suddenly executing PHP code if someone managed to inject one. It also gives you a lot more options as to how to display the page name.
  • Spudley
    Spudley over 13 years
    @Demla Pawan: If we're still talking about .php v .html in the URL then it makes no odds to the end user. To the server, modifying .htaccess (or better, the main server config) will be more efficient as it will access the named file directly and won't have to run every URL through a regex parser like mod_rewrite. The mod_rewrite option would however, mean that both .php and .html versions of the URL would work if the user browsed to them. For more complex URL solutions such as the SEO technique I described, mod_rewrite is the correct answer, as this can't be done simply by renaming the file.
  • Peter Mortensen
    Peter Mortensen over 4 years
    How can URL rewriting be done (in this case)? A relevant (sufficiently specific) link might be enough.