Apache 2 and .htaccess are downloading, not running, PHP files - php

I'm trying to use .htaccess files with PHP on OS X (Snow Leopard, 10.6.8), but when I go to localhost/ the browser just downloads whatever file it's lead to by the rewrite rules (rather than getting the output of the server-side execution of the PHP).
I've confirmed that PHP runs with Apache on other, simpler installations. For debugging I'm targetting check.php script so that should be good.
The web is full of guidance to hunt down and replace all AllowOverride None statements with AllowOverride All, but my apache configuration for this differs from the base OS X installation and doesn't include any of the various files with the AllowOverride None directives. I've set all of those touched by my configuration.
This same configuration runs the php scripts just fine when they're hit by the URL.
I've confirmed that mod_rewrite.so is available and loaded.
I'd appreciate help diagnosing / debugging this.
EDIT:
Also, when I hit the page from Firefox, I get a dialog saying "You have chosen to open [blank, for the index url] which is a: application/x-httpd-php5" and the choice to save or run. That x-httpd-php5 value is the string I use in the AddType directive in the .htaccess file.

Make sure Web Sharing is turned on in the preferences.

Related

Display all files in MAMP htdocs through browser

I recently purchased a new MacBook Pro (10.8 OS) and installed MAMP 3.0 (not MAMP-Pro) but I have been searching the web on how to display all files when viewing a folder within the htdocs directory such as: htdocs/stackoverflow VIA the browser (Chrome or Firefox). This is a feature that I do not have a problem with in Windows using either WAMP or XAMPP when navigating to the localhost/directory/contents. I do understand that localhost must be accessed through locahost:8888 or whatever port it has been modified to. I do not have an issue starting or stopping the MAMP server and everything is executable through NetBeans 8.0 when I set a .php file as the index:
So just to be clear, if I have a directory under htdocs (htdocs/foobar/) filled with several .php files I want to be able to view them in the sub-directory of htdocs instead of a blank browser (tested within Chrome and Firefox). I would imagine this is a security setting I am missing in the configuration? How would I enable, for local development, the ability to view all files, directories, and contents VIA the web browser? If it helps or may be an issue I am using NetBeans 8.0 as my IDE for PHP.
Windows:
localhost
-stackoverflow
--foo.php
--bar.php
--humpday.php
Mac:
localhost:8888
-stackoverflow
--empty in browser (chrome or Firefox)
I have searched to see if this a php.ini feature, MAMP 3 documentation has nothing on this, and Netbeans shows nothing per the search.
Ok after much research and the help from Kevbot and Matt Thompson I was able to figure out what to do and it is as followed:
You should enable all hidden files in Mac that are default hidden. To do this open a terminal (Finder > Applications > Utilities > Terminal) I originally referenced this site but it was wrong in regards to showing hidden files for OSX 10.8:
WRONG:
defaults write com.apple.Finder AppleShowAllFiles YES
RIGHT:
defaults write com.apple.finder AppleShowAllFiles YES
After doing so I held down the option + clicking Finder at the same time to prompt Relaunch of Finder.
You will need to navigate to MAMP (in this case MAMP 3.0 non-pro) in the Applications folder to MAMP > conf > apache > httpd.conf.
Open file in a text editor and search for Options Indexes. It was line 202 for me.
Change:
<Directory />
Options Indexes FollowSymLinks
AllowOveride None
</Directory>
TO:
<Directory />
Options Indexes FollowSymLinks
AllowOveride All
</Directory>
Create an .htaccess file in the desired directory and add:
Options +Indexes
IndexOptions +FancyIndexing
Launch/relaunch MAMP. Do note that if you have an index anything (.php, .html. .xhtml, etc. etc.) will show this instead of the directory listing
Actual Answer:
You need to modify the .htaccess file in your root directory.
I was able to get this to work with no issues. In your .htaccess, add the following:
Options +Indexes
IndexOptions +FancyIndexing
DirectoryIndex somethingRandom.html
Here is what each line does:
Line 1 specifies to allow the indexing of files.
Line 2 tells the browser to display more information regarding the files
Line 3 tells the browser the default index file is not index.php or index.html. Just set the file to something that will never exist.
Old Answer:
There are several things you can do to configure MAMP.
You don't have to access MAMP with localhost:8888, you can access it with just localhost with the following changes. If you open the MAMP program, and select:
Preferences
Ports
Set to default Apache and MySQL ports
Then, you can access your server through localhost in the web browser. Also, if you want to switch development folders (using a subfolder of htdocs as it's own site) you can configure those as well. Select the following from the MAMP program window:
Preferences
Apache
Select (a folder inside of htdocs)
Now, when you access localhost in the browser, that folder will be your root folder until you change it back to htdocs.
And just to make sure, did you remember to "Start Servers?"
Hope this helps.

Zend Server "Caching" of JS and CSS files

I'm running Zend Server in a CentOS VM on Vitrualbox and I'm having a problem with linked JS and CSS assets being "cached". I say "cached" because they aren't, in the true sense, being cached, but rather when I add content to a JS or CSS file they become corrupted and the changes do not appear. Instead the file is appended with a bunch of bad characters eg.
layout.phtml (zend framework template)
$this->headScript()->appendFile ('/js/admin/product.js', 'text/javascript' );
This renders:
<script type="text/javascript" src="/js/admin/product.js"></script>
products.js
//re-add scrolling handles
scrollThumbs.reSortThumbs(ul);
product.moveFileInput(ul);
};
};��������������������������������������������������
If I remove content from the JS or CSS file the result is an incomplete file and not the addition of bad characters as outlined above.
I've turned off all forms of Zend caching and even turned off Zend Optimizer. I've deleted browser cache and tried several browsers.
I have ssh'd into the server and double checked the file and it is perfectly formatted and contains the changes. I've tried restarting Zend Server (/usr/local/zend/bin/zendctl.sh restart) and Apache (service httpd restart)
The only way to fix it is to restart the entire OS (reboot). Interestingly, if I remove the changes, it goes back to working correctly. I can only assume that there is some form of caching happening somewhere on the server side.
It turns out that it is a Virtualbox shared folder issue and not one uniquely related to Zend Server, but Apache in general.
The fix came from Shared folder in VirtualBox for Apache
Add EnableSendfile off to your vhost file eg.
<VirtualHost *:80>
DocumentRoot "/mnt/your/shared/dir"
ServerName Default
<Directory "/mnt/your/shared/dir/public">
EnableSendfile off
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
It is because you have a browser cache turned on and the file name stays the same.
This can be avoided by using md5_file() on you scripts and saving this to cookies,
and on each request check if the cookie changed - in this case you will be able to manage cases when your front-end files are changed.
I tried the EnableSendfile off thing but it does not work on my Centos virtual machine with ZendServer. So I moved to my ubuntu virtual machine with apache installed manually (without ZendServer) and it worked perfectly. What seems to me, is that it is a issue on ZendServer, at least on ZendServer configuration.

Returning Http Status Code to Apache from PHP?

On my local development machine, I downloaded PDT + Zend Server, which included Apache 2.2.16 and PHP 5.3.5, running on Windows 7. On my local site, I included a .htaccess that includes ErrorDocuments for 404, 403, and 500. In my PHP, I use header("Http/1.0 404 Not Found") when the user requests a document that doesn't exist. On my local server, everything works great. My custom ErrorDocument appears and I'm happy.
I upload the everything to my shared host running Apache 2.2.38 and PHP 5.3.8 on a Linux server, and suddenly the ErrorDocuments only work if they don't come from PHP.
Is there some setting in PHP.ini or httpd.conf or .htaccess that allows Apace to see the error codes from PHP, which makes my dev server work correctly, but not my shared host?
In researching this, all I ever saw was "Apache doesn't see the status code once it passes off to PHP." In such a case, why does my dev server work right?
Edit
For clarity, here's the .htaccess:
# Use PHP 5.3
Action application/x-hg-php53 /cgi-sys/php53
AddHandler application/x-hg-php53 .php
#Deny Include Files
<Files *.inc>
order deny,allow
deny from all
</files>
#Provide custom error documents
ErrorDocument 404 /Errors/Http404.php
ErrorDocument 403 /Errors/Http403.php
ErrorDocument 500 /Errors/Http500.php
The .htaccess works because if the user navigates to myhost.com/jdkslfjdls the user receives the content of Errors/Http404.php.
However, if the user navigates to myhost.com/images/GetImage.php?Id=5 (when there is no image #5) the user receives no content.
If they navigate to (Internal IP Address)/images/GetImage.php?Id=5, the user receives the content of Errors/Http404.php.
A similar problem occurs if the user tries to access GetImage.php?Id=6 (when there is an image #6 but they don't have permission). On the shared server, they get a blank page or the browser's 403 error. On my dev server, they get my actual custom 403 error page.
Again, 403 error document works on the shared server if I try to access a .inc file.
Are you sure that your .htaccess file is used on the server? Sometimes the hosting company does not allow the global apache settings to be overwritten by the local .htaccess files.
If you however have access to your virtualhost configuration than you may want to look at the following directive
AllowOverride All
If instead you find
AllowOverride None
try to change as above.
Check if you have
AllowOverride ALL
in your apache.conf under the Directory directive inside the VirtualHost corresponding to your website
Anyway, that may be one possible cause, the other may be to check whether or not you have enabled mod_rewrite
On ubuntu or debian based systems, you'd simply link or copy the mod_rewrite.load from /etc/apache2/mods-available/mod_rewrite.load to /etc/apache2/mods-enabled/mod_rewrite.load
Rewrite should have nothing to do though, but I've seen cases where your .htaccess directives may require mod_rewrite
So check for those things in apache conf files.
If nothing happens contact me # phpcip#gmail.com
Hope it works.

Each time I view a php page on localhost, it downloads the page

I have altered the httpd.conf file good to my knowledge by adding index.php in the directory index,
LoadModule php5_module "d:/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
PHPIniDir "d:/php"
But any .php file I load, it downloads it.
Any help would be appreciated.
Thanks
Jean
The browser is not the issue - it is something with your configuration. Where have you placed this code in your httpd.conf file? Is it nested within other (perhaps module-specific) code? Maybe you can provide more (or all) of your http.conf file, and anything relevant in apache's startup log.
If you're not familiar with configuring apache, you may want to consider using a distribution of LAMP/WAMP as those will work out of the box.
But there is definitely something wrong with your apache configuration. For one reason or another (in apache's config), your PHP is not being parsed. This could be because the PHP isn't installed properly, because you've put the code to enable it in the wrong spot where it isn't being invoked, because you need something more than that for your specific environment, or because you have something conflicting with the code.

Apache rewrite rule forces download

I'm trying to port a PHP site developed by another coder (who is no longer around) and I'm having a problem with the Apache Rewrite rules which are prompting a file download on the target server. I'm sure this is a simple problem, but I'm having some difficulty Googling an answer. I'm running on a (dedicated) Ubuntu Server with a standard installation of Apache and PHP5 and porting from shared a shared server where everything runs fine. No site files have been altered during the port.
The .htaccess file contains this code (only)
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
Options -Indexes FollowSymlinks
RewriteEngine on
RewriteRule ^html/(.*) /index.php?init=site\/$1\/$2\/$3\/$4\/$5\/$6\/$7\/$8\/$9
RewriteRule ^mykart$ /index.php?admin=true
RewriteRule ^mykart/$ /index.php?admin=true
RewriteRule ^mykart/(.*)$ /index.php?init=admin\/$1\/$2\/$3\/$4\/$5\/$6\/$7\/$8\/$9&admin=true
When I try to open the file http://www.mysite.com/html/#home the browser attempts to download the (index.php) file instead of displaying it, with the message
"You have chosen to Open
[dialog shows blank space here]
which is a: application/x-httpd-php
from....
"
I guess I must have missed something in either the PHP or Apache configuration, but what?
EDIT: To clarify, the server is running Apache2 and has several, functioning, PHP sites on it. Furthermore if I delete the .htaccess file and run a simple phpinfo display page everything runs fine, so it's not the execution of PHP per see.
I suppose that the MIME type application/x-httpd-php5 is not valid. I’ve tried it on my local machine and it caused the same behavior.
Have you tried application/x-httpd-php instead?
Looks like an Apache config issue, of course I could be wrong. Have you checked httpd.conf for the following lines:
# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php
AddType text/html php
I had a similar issue. Browser attempted to download links from php website, instead of loading them.
It wasn't Php interpreter issue for me, it turned out to be misplaced .htaccess file. However, I didn't realized that disabling the htaccess file solved the issue for hours, due to browser cache.
So, don't forget to clear your browser caches! And restart Apache.

Categories