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

后端 未结 4 1077
夕颜
夕颜 2020-12-06 14:40

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

相关标签:
4条回答
  • 2020-12-06 14:47

    Rewrite Info here

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

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*)\.htm$ $1.php [NC]
    
    0 讨论(0)
  • 2020-12-06 14:50

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

    0 讨论(0)
  • 2020-12-06 15:00

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

    RemoveHandler .html .htm
    AddType application/x-httpd-php .php .htm .html
    
    0 讨论(0)
  • 2020-12-06 15:05

    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.

    0 讨论(0)
提交回复
热议问题