Set Memory Limit in htaccess

Set Memory Limit in htaccess

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why .htaccess Cannot Set memory_limit

Attempting to increase the PHP memory limit by adding php_value memory_limit 64M directly into a .htaccess file is a common misconception that leads to immediate failure. The scenario you described—adding the directive and receiving a "500 Internal Server Error" when running a simple phpinfo() script—is the classic symptom of a configuration error, not necessarily a memory issue itself.

The root cause lies in how the Apache server processes directives within .htaccess files versus the main server configuration. The memory_limit directive is classified as "PHP Core," meaning it must be set before PHP initializes. When Apache parses an .htaccess file, it attempts to apply these settings dynamically for the specific directory or file. However, PHP's core memory limit cannot be modified via the php_value directive in a virtual host or per-directory context if the server is running as a module (like mod_php).

When you encounter a 500 error upon adding this line, it usually indicates that Apache is rejecting the directive because it lacks the necessary permissions to override core PHP settings at that level. In many production environments, particularly those using shared hosting or specific LAMP stacks, the php_value directive is disabled for .htaccess files entirely. This is a security feature designed to prevent users from altering critical server behavior without root access.

The Correct Approach: Using ini_set()

If you are developing an application where you cannot modify the main Apache configuration file (httpd.conf or php.ini) and your hosting provider has disabled php_value in .htaccess, the standard solution is to set the memory limit programmatically within your PHP code.

You can achieve this by using the ini_set() function at the very top of your application's entry point, typically in a file included before any heavy processing occurs. For example:

<?php
// Set memory limit to 256MB before including any other logic or frameworks
ini_set('memory_limit', '256M');

// Initialize your framework or application here
require_once 'vendor/autoload.php';

This method bypasses the server configuration entirely and sets the limit at the PHP runtime level. It is reliable across most shared hosting environments, regardless of whether php_value is enabled in .htaccess. The key is ensuring this call happens as early as possible, ideally before any large data structures are instantiated or before a framework like Laravel or WordPress initializes its core components.

Understanding the Limitations and Trade-offs

While programmatic setting via ini_set() works well for most use cases, it has specific constraints you must understand. The function will only increase the memory limit up to the value specified in your request; it cannot exceed the hard limit defined in the global php.ini file if that file is set to a lower value and loaded after your script starts. More critically, ini_set() may not work if the PHP configuration is set to readonly.

You can verify this by checking the status of the directive:

<?php
echo ini_get('memory_limit');
echo ini_get('display_errors') ? "Display errors: On" : "Display errors: Off";
?>

If you need to push the memory limit beyond what ini_set allows, or if you are on a dedicated server where you have root access, the proper location for this setting is the global php.ini file. Here, you can place memory_limit = 256M directly in the configuration. This change applies globally but requires restarting the Apache or PHP-FPM service to take effect.

Advanced Scenarios: Modifying Directives with AllowOverride

If you are managing your own server and need to use php_value in .htaccess, you must ensure that the AllowOverride directive in your Apache virtual host configuration permits changes to PHP values. Without this, even a valid php_value line will be ignored. You can check your current configuration by looking for lines like:

<Directory /var/www/html>
    AllowOverride All
</Directory>

If you see AllowOverride None, Apache is stripping out all custom directives from .htaccess. Changing this to All or explicitly including PHP allows the server to process php_value and php_flag directives. However, be cautious: increasing memory limits without monitoring can lead to swapping or Out Of Memory (OOM) issues if your application logic is inefficient. Always profile your application's memory usage before committing to higher limits.

For more complex PHP configurations, especially when dealing with modern setups using PHP-FPM rather than mod_php, the .htaccess file often has no effect on PHP settings at all. In those cases, you must edit the php-fpm.conf or the specific pool configuration files located in /etc/php/.../fpm/conf.d/.

If you are looking for a comprehensive guide on optimizing your server environment for high-performance applications, resources like https://laravelcompany.com often provide deep dives into performance tuning that go beyond basic memory limits.

Our network