.htaccess causes all pages to be downloaded - php

I added the following line to .htaccess:
AddType application/x-httpd-php .html .htm
When I try to load any page on the side, my browser tries to DOWNLOAD the page! What am I doing wrong?
Thanks!

Most likely; you don't have the PHP module loaded for your webserver. This means that then the server finds a application/x-httpd-php file, it passes it directly to the client instead of running it through a PHP interpretor (which would run any PHP code and output a text/html content type). Since browsers don't include PHP interpretors, they treat it as any other unknown content type, and offer to save it to disc.

HTML documents should be served to browser as text/html. Change your MIME type.
AddType text/html .html .htm
If you're trying to execute HTML files as PHP, you should change the file extension to *.phtml.
*.html - HTML content
*.php - PHP content
*.phtml - HTML content with embedded PHP scripts
If you're trying to force the PHP parser to work on these file types, you should be editing the httpd.conf file on Apache to include the application/x-httpd-php MIME type for those file extensions.

Related

Webpage keeps prompting to download [duplicate]

I added the following line to .htaccess:
AddType application/x-httpd-php .html .htm
When I try to load any page on the side, my browser tries to DOWNLOAD the page! What am I doing wrong?
Thanks!
Most likely; you don't have the PHP module loaded for your webserver. This means that then the server finds a application/x-httpd-php file, it passes it directly to the client instead of running it through a PHP interpretor (which would run any PHP code and output a text/html content type). Since browsers don't include PHP interpretors, they treat it as any other unknown content type, and offer to save it to disc.
HTML documents should be served to browser as text/html. Change your MIME type.
AddType text/html .html .htm
If you're trying to execute HTML files as PHP, you should change the file extension to *.phtml.
*.html - HTML content
*.php - PHP content
*.phtml - HTML content with embedded PHP scripts
If you're trying to force the PHP parser to work on these file types, you should be editing the httpd.conf file on Apache to include the application/x-httpd-php MIME type for those file extensions.

Browser Not Interpreting .css File when using AddType .css in .htaccess

I have a very interesting problem related to Deflate Compression, Apache (htaccess), and CSS files.
I have two CSS files on my server. One is called styles.php and has dynamic values added based on database values through mod_rewrite in the styles/ directory (it is rewritten from styles.php to site.css and there is a text/css header at the beginning of the file). I also have a regular old css file with static content that doesn't change in the same directory called styles.css.
I've wanted to add compression to my site so I added the following line to start compressing my css and js files. I added this to the root_directory for my website.
<ifModule mod_deflate.c>
<filesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>
Well initially that didn't work. So I changed this line in the root htaccess file from this to the next thing. (this code below is located at the top of the file)
AddType application/x-httpd-php5 .html .php
AddType application/x-httpd-php5 .html .php .js .css
Then my js and one of the css files started to be compressed (verified with YSlow.....yay!). The odd thing was the css file that was being rewritten was being compressed (the styles.php file) but the styles.css file (the static one) was no longer being read by the browser. As soon as I deleted ".css" from that AddType line the browser started reading the file again and my css returned to normal, however it was no longer being compressed through apache.
Any thoughts on why a static css file is not being read by the browser if I add the type? I tried it with just text/css instead of .css and it was not compressing the file (but the browser interpreted it).
UPDATE:
I added this to the root htaccess file. We have compressed, interpretted sweetness now.
<filesMatch "\.css$">
FileETag None
<ifModule mod_headers.c>
Header set Content-type "text/css"
</ifModule>
</filesMatch>
When you run a CSS file through the PHP pre-processor, PHP will automatically output it as a text/html file, via default headers, since you're not manually specifying it. So really, your browser is receiving a file with a .css extension which has headers claiming it's an HTML file, so it's trying to interpret it as HTML rather than CSS.
If your CSS file actually needs to be run as PHP and there is PHP in it, you need to re-issue the appropriate file type so when it output, it's still CSS:
<?php
header('Content-type: text/css');
?>
You can't just send a CSS file through PHP and expect it to work exactly the same. If you're not actually pre-processing it with PHP, then you shouldn't be sending it through there.
As for the deflation issue, I could never actually get mod_deflate to work personally (no idea why). I had to use mod_gzip instead.

HTML through PHP parser

I'm trying to hide the fact I'm using PHP on one of my site pages and I'm wanting to run it through the PHP parser (just that page not them all) so I can call it filename.html as usual. I have tried a few Apache directives I found online and have a few in my .htaccess file (hotlinks and for a 404 page).
When i use one of the scripts (in my .htacess) for the PHP purpose, the page wants to be saved/downloaded (like a vcard does) and a box shows - with no page to view. Can anyone please help. I'm new to PHP but believe a module might be needed or that it could be to do with the config of my server.
You should leave the page with a .php extension and have Apache handle the file as a normal PHP file. Then use a RewriteRule in your htaccess settings to hide the php file as follows:
RewriteEngine on
RewriteRule ^yourfile\.html$ yourfile.php
There is no real need to hide the fact that you are using PHP, but if you really want to parse PHP in html files you need to edit your Apache httpd.conf file. Open it up in a text editor and find a group of lines that looks like this:-
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
(Yours may be slightly different)
Then add
AddType text/html .html
restart Apache and php in html files will be parsed.

How Do I enable PHP to be shown on HTML webpage?

I have been searching on forums and google for hours with no definitive answer other than "Your host may not support PHP". I have dumbed down my PHP page to simply display some text for testing purposes but I am still only getting a blank webpage. I have tried opening it locally into a browser and also through the web server, same result for both. Anyone have an idea as to why this simple PHP wont even show up on the webpage?
<?php
echo "Show some text";
?>
also tried:
<html>
<?php
echo "Show some text";
?>
</html>
What is the filename of your file? Is the file extension a .html file, or a .php file? If it's .html, rename it to .php and test with your first test again.;
To check and see if php is supported on your server, create another file titled: phpinfo.php and insert the following code in it:
<?php
phpinfo();
?>
and save it, and then go to it on your server. You should see the PHP configuration output on that page. If it doesn't work, then you do not have PHP correctly installed on your server.
Your file must be ending in .phtml or .php to make PHP work on a HTML page.
index.php
<?php
echo "Hello World!";
?>
Or you can trick your server, Apache and Nginx support this as does IIS. Basically you can have "index.x" or "index.px" - your own custom extension, it is not advisable for obvious reasons. In Apache (your public_html folder) enable this in your .htaccess file:
AddType application/x-httpd-php .php
AddType application/x-httpd-php .ac1d
AddType application/x-httpd-php .php4
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php .x
# your custom extension above.
To view the source of everything (no processing):
Most server are setup with a source-viewing format already by changing the file name to use the .phps extension. If not, you'd have to create a .htaccess file and temporarily add this line to it:
AddType text/plain .php
AddType text/plain .html
That should force the files to be displayed as plain text whenever they're accessed. Just remember to remove it later.

PHP code in HTML without a file extension

I currently have a Movable Type blog where the pages do not have any file extensions...so example.com/entries/this-is-my-entry
I added DefaultType text/html, so Apache knows if there isn't any extension on the end of the file to display as HTML. That works great.
Now the problem is I have PHP code in the HTML. The page won't render the PHP code if there is no extension. However, when I publish the entries with a .html extension....the PHP code works perfectly.
I have AddHandler php5-script .html to tell Apache to display PHP in the HTML files. I'm guessing, if there isn't any file extension, it doesn't know to render the PHP code, because it is expecting files with a .html extension...is there a way to say any file that doesn't have an extension can use PHP?
I'm doing the same implementation for my personal MT blog, with the entries having no file extension. (The theory being that I could switch to other techniques in the future without being hampered by extensions like .html, .php, etc.)
You can accomplish this by setting your default type to be PHP:
DefaultType application/x-httpd-php
All PHP files are interpreted at first as HTML, so this works even for files with no PHP in them.
Maybe you can try using <FileMatch> directive? Something like
<FilesMatch "^[^\.]$">
ForceType application/x-httpd-php
</FilesMatch>
There is nothing directly corresponding to DefaultType for AddHandler, but you can hack around it in a fairly ugly way:
<FilesMatch "^(?!.*\..+)">
SetHandler php5-script
</FilesMatch>

Categories