6

I am using linux server and in my server I have install PHP 7.* version. I want to use PHP code in HTML file. Right now it render PHP code in in web page. I am using the following code in my .htaccess file but it not working.

AddHandler x-httpd-php .html .htm

and

AddHandler php7-script .php .html .htm

and

<FilesMatch "\.html?$">
SetHandler application/x-httpd-php7
</FilesMatch>

But all are these not working.

Alok Bichhwe
  • 172
  • 2
  • 8
  • Are you running Apache or Nginx? If Apache, try: `AddType application/x-httpd-php .html .htm`: https://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – M. Eriksson Jun 13 '17 at 05:26
  • @Magnus Running Apache. When I am using AddType application/x-httpd-php .html .htm it downloads the file with the html code. – Alok Bichhwe Jun 13 '17 at 05:31
  • You can only use .htaccess files when [AllowOverride](https://httpd.apache.org/docs/2.4/en/mod/core.html#allowoverride) in the parent Directory is set to true. Also, are you sure the php module is loaded in the Apache? Check enabled-modules directory. – Gordon Jun 13 '17 at 05:33
  • @Gordon If it tries to download the file when the OP is setting the AddHandler in htacess, then it's safe to assume that the htaccess was loaded and used. Otherwise, it would simply be ignored without any change. – M. Eriksson Jun 13 '17 at 05:37
  • 1
    @MagnusEriksson yeah, but if there is one thing I learned at StackOverflow, then that it's safer to verify than to assume though. – Gordon Jun 13 '17 at 05:39
  • Check how the PHP module in Apache is registering the `php`-extension (check the `php7.conf` in the Apache mods-folder) and use the same for htm and html? Where you find it depends on the linux dist (default for Ubuntu is: `/etc/apache2/mods-available/...` – M. Eriksson Jun 13 '17 at 05:41
  • I have check the addition module list with the phpinfo() function. There has no module loaded in this list. So I need to load it first? – Alok Bichhwe Jun 13 '17 at 05:43
  • The php module for Apache, not a php library. If that wasn't loaded, Apache wouldn't be able to parse PHP-files. – M. Eriksson Jun 13 '17 at 05:44
  • Why you want to write `PHP` code into `HTML` file instead of writing into `PHP` file? – Syed Aqeel Jun 13 '17 at 06:14
  • I have done it by adding AddType x-mapp-php4 .html .htm and remove SetHandler application/x-httpd-php7. @FahadKazmi I got the code from any other prerson, I have no idea why he use php in html. – Alok Bichhwe Jun 13 '17 at 10:30
  • What web server (incl. version) do you use ? Apache2 ? –  Jul 16 '18 at 18:52

1 Answers1

1

After you installed php7.0-cgi

sudo apt install php7.0-cgi

you can add to your .htaccess

AddHandler php70-cgi .php 

tells Apache to run PHP on any file with the extension ".php" using the Module called php70-cgi that is afaik modules/php70-cgi.so

A reason why its not working could be the webserver settings in

/etc/apache2/sites-available/default

if there is AllowOverride „None“ set it to „All“ else you can only make setting in <Directory> and not in .htaccess

<Directory /var/www/>
    ...
    AllowOverride All
    ...
</Directory>