How do I add PHP code/file to HTML(.html) files?

后端 未结 12 2098
栀梦
栀梦 2020-11-21 22:05

I can\'t use PHP in my HTML pages. For example, index.html. I\'ve tried using both:

 

and



        
相关标签:
12条回答
  • 2020-11-21 22:53

    In order to use php in .html files, you must associate them with your PHP processor in your HTTP server's config file. In Apache, that looks like this:

    AddHandler application/x-httpd-php .html
    
    0 讨论(0)
  • 2020-11-21 22:54

    Add this line

    AddHandler application/x-httpd-php .html
    

    to httpd.conf file for what you want to do. But remember, if you do this then your web server will be very slow, because it will be parsing even static code which will not contain php code. So the better way will be to make the file extension .phtml instead of just .html.

    0 讨论(0)
  • 2020-11-21 22:54

    MODERN REVISION: you also now need to fix it by changing the little known ''security.limit_extensions' in php-fpm doing this. You will probably already know the well documented mechanism of changing apache to AddHandler/AddType I will not go into that here.

    1. YOU MUST find out where php-fpm/conf is in your set up. I did it by doing this

      # grep -rnw '/etc/' -e 'security.limit_extensions'

    2. I got this back

      '/etc/php-fpm.d/www.conf:387:;security.limit_extensions = .php .php3 .php4 .php5 .php7'

    3. go to this file, edit it and MAKE SURE YOU REALISE IT IS COMMENTED OUT EG: change it from ';security.limit_extensions = .php .php3 .php4 .php5 .php7' <- NB: note the ";" - this line is actually DISABLED by default - and you do not need all the nonsense extensions, in fact they are probably dangerous.. change it to 'security.limit_extensions = .php .htm' <- note the semi colon is now removed. then restart apache/(or nginx) and restart php-fpm # service php-fpm restart # service httpd restart

    0 讨论(0)
  • 2020-11-21 22:56

    I think writing PHP into an .html file is confusing and anti-natural. Why would you do that??

    Anyway, if what you want is to execute PHP files and show them as .html in the address bar, an easiest solution would be using .php as normal, and write a rule in your .htaccess like this:

    RewriteRule ^([^.]+)\.html$ $1.php [L]
    
    0 讨论(0)
  • 2020-11-21 22:56

    By default you can't use PHP in HTML pages.

    To do that, modify your .htacccess file with the following:

    AddType application/x-httpd-php .html
    
    0 讨论(0)
  • 2020-11-21 23:01

    Create an empty file using notepad and name it .htaccess ,Then copy that file in your project directory and add this line and save.

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

    else save the .html file using .php as php can support html,and save it in computer/var/www/html path(linux)

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