PolurNET Communications Support Forums: [HOW-TO] Fixing ForceType & Other Apache .htaccess Errors (PHP as a CGI) - PolurNET Communications Support Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[HOW-TO] Fixing ForceType & Other Apache .htaccess Errors (PHP as a CGI) Covers changing max file upload size too

#1 User is offline   PN-Anand Icon

  • President, Founder, Chief of Operations
  • Icon
  • Group: PolurNET Staff
  • Posts: 1,816
  • Joined: 27-July 05
  • Gender:Male
  • Location:Canada

Post icon  Posted 01 October 2007 - 10:54 AM

PROBLEM:

When trying to follow a tutorial such as http://www.developer...0330/page1.html , one notices they employ the Apache ForceType directive to achieve static URLs together with mod_rewrite. As we have compiled PHP as a CGI/FastCGI on most servers, using the ForceType directive or anything similar would cause Internal Server 500 errors. We present a few tips/workarounds to ensure you get the intended effect, but one that is compatible with our system configuration

SOLUTION:

CASE #1) First, let's use an example. Say you're using the above tutorial link and come up to the place where the author talks about the following:

Excerpted from Jaisen Mathai, on developertutorials.com, said:

This will make sure that all requests to http://yoursite.com/article will send the file "article" to the PHP engine for parsing.

Let's try it! Make a file named .htaccess and put this in it.

<Files article>

  ForceType application/x-httpd-php

</Files>


Now make a file named article (yes, without an extension) and put this in it.

<?php

  echo 'This thing works!!<br />';

  echo 'Request_Uri is: '.$_SERVER['REQUEST_URI'].'<br /><br />';

  echo 'This could come in handy!';

?>


The above would actually produce an error since ForceType is not compatible with PHP compiled as a CGI with phpsuexec. Fortunately, the workaround below works superbly:

#enter in a .htaccess file
#PHP as a CGI with Suexec .htaccess Style
	<Files article>
	SetHandler application/x-httpd-php
	</Files>


CASE #2) For our second example, let's say you want to edit a PHP configuration variable, such as upload_max_filesize to increase the file uploading limit. We can't use php_value in a .htaccess file, as we have PHP compiled as a CGI, which means Apache .htaccess edits won't affect PHP (and will actually cause errors). Instead, we would employ the php.ini method. Also take note that as opposed to a .htaccess file, a php.ini file needs to be placed in every directory and subdirectory that requires the altered directives.


; place in a php.ini file
; change file upload limit to 8MB
;;;;;;;;;;;;;;;;;;;

upload_max_filesize = 8M;
post_max_size = 10M;

This will increase the file uploading limit on your script (this case to 8MB), if it runs from the same directory where this php.ini file is located. For complex scripts, you'd usually place this php.ini file both in the public_html main directory, and /includes/ directory, if exists.

For Windows-based HostOptima servers, this is a bit different, and requires the following edit:

; place in a php.ini file
; change file upload limit to 8MB with fix (tested code on Windows/IIS servers)
;;;;;;;;;;;;;;;;;;;
upload_max_filesize = 8M;
post_max_size = 10M;
cgi.force_redirect = 0;
cgi.fix_pathinfo = 1;


The need for the above modification is complex, but only necessary in cases where the first method fails for some unknown reason.

NOTES: - If editing php config variables, you can also double-check to make sure the intended change was made successfully, by checking out a phpinfo script. Simply make a new .php file with
<? phpinfo(); ?>
and this will display the PHP info output. Search for the config variable you modified, and if it's different than default, the change was made successfully.
0

#2 User is offline   PN-Anand Icon

  • President, Founder, Chief of Operations
  • Icon
  • Group: PolurNET Staff
  • Posts: 1,816
  • Joined: 27-July 05
  • Gender:Male
  • Location:Canada

Posted 03 December 2007 - 08:37 PM

For our servers with Apache 2.2.x, the above SetHandler function may not work. There is an alternate way that has been tested to work with it below:


Quote

#apply for a single file
<FilesMatch "file.html">
AddHandler application/x-httpd-php5 .html
</FilesMatch>

#apply for all files with following extensions
AddHandler application/x-httpd-php5 .php .shtml .htm .html


This will effectively parse your non-.php extension files with PHP. Note the "x-httpd-php5" which means it is using the Apache php5 module, as Apache 2 changes the way php is installed on our servers.

This post has been edited by PN-Anand: 06 February 2008 - 10:16 AM
Reason for edit:: modification of rules

Anand A.
President, Founder, Chief of Operations
PolurNET Communications
anand @ polurnet.com
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users