Skip to content

Setting the PHP script memory limit in an .htaccess file

The memory_limit directive in PHP defines the maximum amount of memory a script can use. By default, this value is often set to 128 MB. On ruachost.com, you can increase or decrease this limit using a custom .htaccess file when you don’t have direct access to php.ini.

  • Prevents “Allowed memory size exhausted” errors.
  • Supports applications that process large files, images, or complex database queries.
  • Provides flexibility for resource‑intensive scripts.

Steps to Set the Memory Limit in .htaccess

Section titled “Steps to Set the Memory Limit in .htaccess”
  • Log in to cPanel or use FTP/File Manager.
  • Navigate to your site’s root directory (e.g., /public_html).
  1. If .htaccess already exists, open it for editing.
  2. If not, create a new file named .htaccess.

Insert the following line:

php_value memory_limit 256M
  • Replace 256M with the desired memory limit (e.g., 512M).
  • Use M for megabytes and G for gigabytes (e.g., 1G).
  1. Save the .htaccess file.

  2. Create a test file info.php with:

    <?php phpinfo(); ?>
  3. Load it in your browser and search for memory_limit.

  4. Confirm the new value is active.

  5. Delete the test file afterward for security.

  • Increase memory gradually (e.g., 256M → 512M).
  • Avoid setting unlimited memory (-1) unless absolutely necessary.
  • Monitor application performance after changes.