PHP files are downloaded by browser instead of processed by local dev server (MAMP)

56,259

Solution 1

You are applying a mimetype where a handler should be (see documentation on handlers)

Try this instead:

AddType application/x-httpd-php5 .php

EDIT: As you have indicated caching modules are loaded, you could read up on caching and htcacheclean (to clear the disk cache). You can also temporarily use the CacheDisable directive. One other thing that you could also try is to rename the file that you have requested (e.g. index.php -> index.bak), request the file again in the browser (should now 404), then revert and try again.

Solution 2

I've had a similar issue a couple times and renaming the file did not work for me. With OS X Lion I found the right configuration is this:

<IfModule php5_module>
    AddType application/x-httpd-php .php
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
    AddType application/x-httpd-php-source .phps
    <IfModule dir_module>
        DirectoryIndex index.php index.html
    </IfModule>
</IfModule>

The magic that made it work for me is the SetHandler application/x-httpd-php part.

Of course edit the <IfModule php5_module> to your php version.

Solution 3

Just note, make sure you don't have a htaccess file from your live environment accidentally downloaded with other files. Additionally, make sure you match your PHP version when editing htaccess. Wrong version cause same issue-wrong settings.

Here is an example for running PHP7:

application/x-httpd-ea-php71 .php .php7 .phtml

I hope this info can help - it happened to me 8 years after ticket is created :)

Solution 4

Perhaps you want application/x-httpd-php5 instead of application/x-httpd-php5s? (Note the lack of an s at the end.)

Solution 5

in my case deleting or comment out "AddHandler php56-cgi .php" in my root's involved htacces files solved it

Best

Share:
56,259
donut
Author by

donut

Updated on July 09, 2022

Comments

  • donut
    donut almost 2 years

    Everything was going great until I added AddHandler application/x-httpd-php5s .php to the .htaccess file in my local server's document root (which I change frequently depending on the site I'm working with). Since I did that when I visit http://localhost:8888 my browser just downloads the index.php and it's not processed at all, just the raw code. Now I removed that line from the .htaccess file but I'm still having this problem.

    I've found that if I add an alternative entry to my hosts file for 127.0.0.1 the new entry behaves like 'localhost' used to. But if I add the line above to my .htaccess it knocks out that new host as well. I've tried reinstalling MAMP and clearing its caches and all the temporary files I could find. I surfed through Apache's httpd.conf file all to no avail.

    So, to be clear: http://localhost:8888 is experiencing the above problem. If I add a new entry to my hosts file for 127.0.0.1, say 'goomba' and the above line is not in the root .htaccess (and has never been for that host/alias/whatever) then I can access http://goomba:8888 just fine. But if I do add that line to the .htaccess then I have to add yet another entry to my hosts file to get around it even if I remove that line from the the .htaccess file.

    I'm fine with using a different 127.0.0.1 alias (host? what is that called?) but it's bugging me that this is still broken.

    Just to be clear, I'm on Mac OS Leopard (but I'm not using the built in Apache setup, but MAMP).

  • donut
    donut about 14 years
    A few things: I'm on Mac OS Leopard; the Apache server is working for new entries to my hosts file. Please see the updates to my post for more details. Thanks!
  • donut
    donut about 14 years
    I tried that to no avail. Before it was working just fine without anything in the .htaccess.
  • donut
    donut about 14 years
    I've tried that (and again, just now) to no avail. My httpd.conf already has that line (minus the 5), anyways. My problem is that, despite the problematic line mentioned in my question being removed, I'm still having problems. It's like the server has cached the .htaccess file only for localhost, not for other hosts that point to 127.0.0.1. Please added emphasis to my question for clarification. Thank you, anyways!
  • cEz
    cEz about 14 years
    Does the server have any cache modules loaded? Yes, the "5" was left in by accident
  • donut
    donut about 14 years
    It does: file_cache_module, cache_module, disk_cache_module, and mem_cache_module. How could I clear these caches? Would any of these cause this problem? Thanks!
  • donut
    donut about 14 years
    Simply renaming the file, trying to access it by the old name, and then renaming it again as you described worked like a charm. Thank you!
  • Jack
    Jack almost 11 years
    This was the case on OSX Mountain Lion as well.
  • Toni Michel Caubet
    Toni Michel Caubet over 10 years
    where should this be placed?
  • webtweakers
    webtweakers over 7 years
    @ToniMichelCaubet: Late to the party, but place that in /etc/apache2/other/php5.conf and make sure that apache's main configuration (in /etc/apache2/httpd.conf) has an uncommented line (near the bottom) that reads: Include /etc/apache2/other/*.conf.
  • bart
    bart over 6 years
    This worked for me as well macOS Sierra. The odd thing is, I encountered this problem when switching from Mamp 3.5 to Mamp 4. By adding SetHandler application/x-httpd-php things started to work again.
  • whp
    whp over 5 years
    I had a similar issue - htdocs/.htaccess contained the line AddHandler application/x-httpd-ea-php70 .php and it was failing silently. Work around was to remove the line.
  • Cory Fischer
    Cory Fischer over 4 years
    I have no idea why this worked, but after 4+ hours and 50+ google searches it worked! NOTE: I had to change php5_module to php7_module because I'm on php7.3