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

I am moving from a server with PHP 5 to a new server with PHP 7, and I am having issues to tell Apache to parse .html as PHP scripts.
In my .htaccess file I have this line working correctly on my current server with PHP 5:
AddType application/x-httpd-php .html
But on the new server, that directive makes any .html file being downloaded instead than executed as a PHP script.
I know that on the current server with PHP 5 installed, PHP is configured with API set as "Apache 2.0 Handler" whereas on this new server it is configured as "FPM/FastCGI" and I guess that maybe that's the problem? If so, how can I overcome it without having to change that API setting?
I look forward to hearing from you. Thank you in advance for any help!

After hours of research, I ended up reading this page:
https://www.digitalocean.com/community/questions/php-fpm-security-limit_extension-issue
And I fixed the problem by adding this code inside .htaccess:
<FilesMatch ".+\.html$">
SetHandler "proxy:unix:/run/php/php7.0-fpm-[myhostname].sock|fcgi://localhost"
</FilesMatch>
And to avoid the server to give me an 'access denied' error I had to put this line inside /etc/php/7.0/fpm/php-fpm.conf:
security.limit_extensions = .php .htm .html .inc .rss .rdf .xhtml
Then restarted PHP daemon with:
service php7.0-fpm restart
Now everything seems to work fine! Quite complex I'd say, and I think that'd due to the fact PHP is configured with the FPM/FastCGI API.
Comments and thoughts are welcome!
Thanks again to everyone.

Related

How do I process HTML files as PHP on AWS EC2 Linux?

I'm trying to move over to AWS EC2 and I've hit a sticking point. I've spent a full day trying every possible solution I could find on Stack Overflow and elsewhere, but to no avail.
I want to process .htm files as PHP files. Files ending in .php are processed just fine, but I can't get .htm files to be processed as PHP.
If I use this "AddHandler" syntax in .htaccess, nothing happens:
AddHandler application/x-httpd-php .htm
"x-httpd-php" can literally be anything. It doesn't matter. Even this does nothing:
AddHandler application/its-a-fish .htm
Using this "AddType" syntax, on the other hand, always causes the file to be downloaded by the browser instead of parsed as code:
AddType application/x-httpd-php .htm
Here again, what follows "application/" doesn't matter. All of these cause the file to be downloaded instead of processed:
AddType application/its-a-fish .htm
AddType application/x-http-php7 .htm
AddType application/x-http-php73 .htm
When the file is downloaded, the Content-Type in the Response header is whatever comes after "AddType" in .htaccess, e.g.:
Content-Type: application/x-http-php73
So maybe I just haven't found the "application" identifier my PHP is running under?
I've tried literally every code example I can find over a ~10-hour period (especially in these threads) but nothing has worked:
Server not parsing .html as PHP
Parsing HTML files as PHP
http://kb.cloudblue.com/en/115773
I suspect the reason it worked on all my previous servers and not on AWS is because PHP is running as FastCGI on AWS, not an Apache Handler, but I can't figure out how to make it work with FastCGI.
Here are the relevant packages I currently I have installed:
[root#ip-172-31-30-111 etc]# rpm -qa | egrep 'http|php'
libnghttp2-1.31.1-1.amzn2.0.2.x86_64
httpd-tools-2.4.39-1.amzn2.0.1.x86_64
mod_http2-1.14.1-1.amzn2.x86_64
php-pdo-7.3.6-1.amzn2.0.1.x86_64
generic-logos-httpd-18.0.0-4.amzn2.noarch
httpd-filesystem-2.4.39-1.amzn2.0.1.noarch
httpd-2.4.39-1.amzn2.0.1.x86_64
php-json-7.3.6-1.amzn2.0.1.x86_64
php-mysqlnd-7.3.6-1.amzn2.0.1.x86_64
php-cli-7.3.6-1.amzn2.0.1.x86_64
php-common-7.3.6-1.amzn2.0.1.x86_64
php-fpm-7.3.6-1.amzn2.0.1.x86_64
I finally figured this out, thanks primarily to this post:
https://talk.plesk.com/threads/cant-get-php-versions-to-serve-html-as-php.342045/page-2#post-854770
Here's what to do specifically on AWS EC2 Linux:
Add these lines to .htaccess, changing the "Files" section to specify the extensions you want to process as PHP:
<IfModule mod_proxy_fcgi.c>
<Files ~ (\.htm$)>
SetHandler proxy:unix:/run/php-fpm/www.sock|fcgi://127.0.0.1:9000
</Files>
</IfModule>
Change the security.limit_extensions setting in /etc/php-fpm.d/www.conf to allow the extensions you added in .htaccess (plus .php):
security.limit_extensions = .php .htm
Restart the php-fpm service (restarting httpd will not force a reread of www.conf):
service php-fpm restart
If you do #1 without doing #2, you will get an "Access denied" error. Step #2 is what fixes that.

Apache runs php files with or without .php extension

My apache runs php files with or without .php extension, How can I disable this and force apache to run php files only with .php extension. I don't want to use .htaccess, this might be an apache config. does anyone know how to disable this option?
Thanks for the answer but using Addtype as you mentioned did not ever work for me, all by itself, even in PHP 5..
Here's what I've tried that DOESN'T work with this version of PHP:
Options +ExecCGI
AddType application/x-httpd-php .php .nusuffix
AddHandler x-httpd-php5 .php .nusuffix
This worked for PHP 5. Now, does not.
This is for a hosted account, so I must do this via .haccess.
You can check your httpd.conf what extensions are configured, it need need to be following. if there is change in your file you can change it to following and restart the apache
AddType application/x-httpd-php .php
more details : https://wiki.archlinux.org/index.php/Apache_HTTP_Server
please add if anything is missing

php includes in .htm files

I've read everything here and tried almost all the suggestions, nothing is helping.
I've got several static .htm pages that all call common headers & footers via a php include:
<?php include('inc_logo_nav.htm');?>
So I just moved to a new server with all kinds of higher level security and nothing we have tried helps.. the system refuses to allow these include calls unless I change the file extension to .php
On the old server I was using in my .htaccess:
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
But on this new box I had to comment out those lines to get the pages to even come up. The tech guy is stumped too..
back to suphp & seuexec but no nginx
and that didnt help either...
cPanel Server running: CENTOS 6.5 x86_64 vmware – WHM 11.40.
I'm already suffering from loss of traffic, changing all the pages to .php after 20+ years is going to kill the site... Can anyone help here??
Thanks
Have you tried these in your .htaccess?
AddHandler x-mapp-php5 .html .htm
AddHandler x-mapp-php6 .html .htm
I needed them on a server hosted by 1&1 to cause HTML files to be parsed as PHP.

htaccess rule to parse php in html files not working

can't for the life of me work out why this isn't working - on a dreamhost server, I've created an htaccess file and added AddHandler php5-cgi .html .htm to allow php in html files, as per the dreamhost docs. On an identical dreamhost package, I've done the same thing for another site and it worked perfectly, but in this case it just won't parse the php! Is there anything I could have missed here which could be causing the problem? The htaccess is in the web root and isn't being overridden by anything else.
Try this
AddType application/x-httpd-php .html .htm
The above setting will work if the apache is picking php as a module.
AddHandler application/x-httpd-php .html .htm
or
AddHandler application/x-httpd-php5 .html .htm
above will work if apache is executing php as CGI?
there is no harm i would say to put both of the above conditions together but try one over other and see if it work for you.
read more here in this article I wrote.
Is your server using suPHP rather than mod_php or plain php-cgi?
Try using
AddHandler x-httpd-php .html .htm
or
AddType application/x-httpd-php .html .htm
instead.
Postscript
Shared Hosting services use UID based separation on individual accounts from each others. Most use suPHP, but some use suEXEC. They will use one of these. Both act as a su wrapper around php-cgi so you can't tell from the phpinfo()as its PHP scripting engine as this will report Server API CGI/FastCGI in both cases. For CGI initiated scripts, phpinfo doesn't report on the Apache config. You need either to look at your hosting provider's FAQ or possibly try:
<?php
header( "Content-Type: text/plain");
echo system('grep -iR LoadModule /etc/httpd /etc/apache2');
The hosting provider's support forums / FAQ might give specific configuration advice here. Have you tried them?
I've got the same problem as you but I just put this AddType php .html .htm to my .htaccess, it's working.
For me it is working for only like this:
AddHandler php-cgi .php .htm

Apache AddHandler: Run ASP code as PHP

I'm running XAMPP.
Is there a line I can add to .htaccess or http.conf to make ASP be parsed and handled as PHP pages?
I had it setup before but can't remember how to do it. What I've tried so far hasn't worked.
Edit: None of these solutions are working, I've tried everything.
If none of the solution above is ok with you, check your Server API with phpinfo(); method.
If Server API is CGI or FAST CGI change it to Apache 2.0 Handler.You can do it using Plesk Panel or CPanel.
Another option is not to change Server API but .htaccess or httpd.conf : http://avinashsing.sunkur.com/2012/04/19/how-to-map-another-file-extension-to-the-php-parser-through-htaccess/.
But I have solved my problem with the first one.
Maybe try this:
AddHandler php5-script .php .asp .aspx .ascx .ashx .asmx
AddType text/html .php .asp .aspx .ascx .ashx .asmx
Add this to your httpd.conf
AddType application/x-httpd-php .asp .aspx .ascx .ashx
Try adding this line to .htaccess;
AddHandler application/x-httpd-php .asp
Apparently it is not as efficient as adding the equivalent to http.conf but it works even when you don't have access to http.conf.
Adding this line in .htaccess enabled me to execute PHP code written in .asp pages:
AddHandler php5-script .php .asp
Apache does not have a module for ASP processing. You have to use IIS for that. Modern IIS can run PHP so you may do it the other way around, use IIS and plug PHP into it.

Categories