htaccess downloading file instead of loading - php

I would really like my index.html to be able to have a PHP script work on it. I read that you can do this through the htaccess file. I only have access to a subdomain website directory, where I can upload my files through FTP.
The directory did not have a htaccess file, so I created one using notepad: .htaccess and added this to the file:
AddType application/x-httpd-php .html
The problem is, instead of loading the index.html page, it downloads it as a file...would I need to add something extra to the htaccess file? :S

You don't need to name the file index.html to have it served by default. You can change the default document using your with an entry in your .htaccess file like this:
DirectoryIndex index.php
Then when you navigate to http://yoursubdomain.example.com you will be served index.php instead of index.html.
If really do want PHP to interpret your .html documents then the entry you had in your question will work when PHP is running as an Apache module. If your host is running PHP as CGI, you want:
AddHandler application/x-httpd-php .html
If it still doesn't work, then this web page has some more suggestions:
http://www.velvetblues.com/web-development-blog/how-to-parse-html-files-as-php/

The directive you have sets the content-type of files with a .html file extension.
If the server has PHP installed and enabled, that content-type will cause it to be run though the PHP engine and then the output from that sent to the client.
If it doesn't have PHP installed, then the file will just be served up to the client with that content-type. Since browsers don't handle PHP scripts themselves, they will then just save the file.
You need to install and enable PHP as well as setting the content-type.

Presumably your hosting is supporting PHP?
If so, then you need to rename your file from index.html to index.php

Related

.htaccess file causes .php to be downloaded

I made a .htaccess file. And I upload file in a index.php, index.php was downloaded instead of being run as a php file. I think the .htaccess file makes php downloadable.
The php file was on the same path with .htaccess file.
The problem is solved , but I wonder why that is downloaded.
My .htaccess file is this.
AddType application/x-httpd-asp .php
That directive tells Apache to process PHP files using ASP. Since it apparently can't process ASP (likely since ASP is not installed by default), it then moves to send headers to instruct the browser to download the file (which is the default for anything Apache doesn't know how to process).
It's not normal to have a directive like that

Why doesn't this html code work?

Given the following index.html file:
<html>
<body>
<p>Welcome to <?= $_SERVER ['HTTP_HOST']; ?></p>
</body>
</html>
I expect to see "Welcome to EXAMPLE.COM", All I see is "Welcome to ".
What would cause this to happen if the code checks out?
Your file is named index.html. Unless you told your server that .html files should be treated as PHP scripts, that means the PHP code is NOT being executed - it's going out as literal text. And since PHP tags make it look like HTML, your browser is properly hiding that unknown/illegal tag.
Rename it to index.php.
It's not running because that part is not html (it's php) but you have saved it as an html file instead of as a php file.
rename it to index.php and try again?
Web servers are usually configured to run PHP only on files with the .php extension. Your index.html file will be passed as-is to the browser, which will probably ignore the unknown PHP tags. If you take a look at the source code of the web page, the tags will probably be there.
If you must have a .html extension, you can usually configure the web server to run PHP on .html files. For example, in Apache, you can use the AddType directive in an .htaccess file or in the server configuration (httpd.conf):
AddType application/x-httpd-php .html
However, this will run PHP on all .html files (in that directory), which may put an unnecessary load on the server.
A much better way is to use URLs without extensions. In Apache, you can use the DirectoryIndex directive to specify a list of index files that the web server will search for:
Options +Indexes
DirectoryIndex index.html index.php
When a browser requests a URL that ends with a slash, such as http://mydomain.example/foo/, the server will search for foo/index.html or foo/index.php in the DocumentRoot (or, failing both, generate a directory listing). You can now use whichever type of index file is appropriate for the moment, without ever having to change your URLs.

what does .htaccess with line AddHandler php5-script .php do?

I am with new web host. The public_html folder of each domain I create is auto generated with an .htaccess that has the following line:
AddHandler php5-script .php
What is this for?
This just instructs PHP to handle files ending in .php by passing them to the PHP5 interpreter. Without this configuration in place, the web server may serve the files to the end-user's web browser as raw PHP code, rather than executing the code. That raises the dangerous possibility of exposing database login credentials or, or other secrets.
Using the same mechanism, you could configure the web server to parse files with other extensions besides .php as PHP scripts and hand them to the PHP interpreter. This is occasionally done to mask PHP scripts by naming them with .html extensions, for example.
# Interpret both .php & .html as PHP:
AddHandler php5-script .php .html
It tells php to handle any file with .php in the filename, even if it's not at the end. A file named smile.php.gif will be interpereted as a php file, which is bad if you are going to be using an upload script. This is because Apache allows multiple extensions in any order, so gif.php.jpg is the same as gif.jpg.php. I have heard the best way to select the handler is with FilesMatch. Of course if your web host has this in their httpd.conf you would have to 'remove' it using your htaccess before using the FilesMatch if you don't have access to httpd.conf.
The answer is that the htaccess tells the webserver to handle the php as php5-script and execute it.
Regarding the first answer, you will achieve your goal but it is a really bad practice and you should not allow html files to be executed as php due to huge security concerns.

htaccess - parsing php into html

I recently played with a .htaccess file to make one server to parse PHP files. Yesterday I uploaded the same .htaccess file and tried to test a PHP file. But something went wrong: visiting my page the browser offers to download the the html page rather then viewing the page!
On the server the filenames end in .html.
I added the following to my .htaccess file:
AddType application/x-httpd-php .html
I tried to find the htaccess file, but once uploaded it just disappears from the root dir.
I tried to upload other scripts I've found browsing. I even tried to search for some problem on a hosting forum. Nothing helped.
Please help!
Try this
AddType application/x-httpd-php .php .htm .html
OR
AddHandler application/x-httpd-php .php .htm .html
And remove other overriding handlers for application/x-httpd-php after above code.
Right, firstly things first, what host do you use?
Also what ftp client you are using? Some by default won't display files starting with . such as .htaccess and .htpasswd, that's why it may appear that you didn't upload it. Also it might be that you don't have the rights to upload in the very root directory, try to go one directory up.
Also from my experience, hosts won't allow you to modify headers via .htaccess this way, because the allowoverride directive is off; instead have a look at url rewrites (via mod_rewrite), which allow you to do the same thing without modifying headers.
Your rewrite .htaccess file might look something like:
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)\.html$ $1.php [NC]
(Not tested though)
Using a rewrite will also mean that your files will in fact maintain the php extension, however they will be access via urls that include .html extension.

Apache not handling files correctly (Handler Help)

I'm trying to set up my .htaccess file correctly and I'm having an issue.
The only thing my .htaccess file at the moment is:
AddType application/x-httpd-php .php .html .htm
This is included because my server is not parsing php in my html files.
However when this is included in my .htaccess file, when I open a page in my browser, the user is prompted to save or open the file locally.
I believe the answer to my issues is setting up an action to be done (run with php) however I cannot find out the path to my php files.
Any help is appreciated.
You will need to edit the configuration for enabled modules. On a Debian/Ubuntu type system this will be in /etc/apache2/mods-enabled The file you are looking for is php5.conf
So far all you have done is specify that (dot) htm, html or php files should be served -by default- as application/x-httpd-php, and to my knowledge there is not a single web browser that would attempt to interpret such content -- hence the save-as dialog.
Either you could fix your .htaccess file not to be broken (it is broken behaviour to serve html files as application/x-httpd-php), or you could manually output the correct HTTP headers using the PHP header() function.
Unfortunately, everyone seems to love abusing AddType (and then complain e.g. that MultiViews is broken). See this article, please.
This is not supposed to work in all cases. It depends on the AllowOverride directive of the web server.
You shoud specify the AddType in the serveur config file rather than in the htaccess.

Categories