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.
Why Change the Memory Limit?
Section titled “Why Change the Memory Limit?”- 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”Step 1: Access Your Hosting Account
Section titled “Step 1: Access Your Hosting Account”- Log in to cPanel or use FTP/File Manager.
- Navigate to your site’s root directory (e.g.,
/public_html).
Step 2: Create or Edit .htaccess
Section titled “Step 2: Create or Edit .htaccess”- If
.htaccessalready exists, open it for editing. - If not, create a new file named
.htaccess.
Step 3: Add the Memory Limit Directive
Section titled “Step 3: Add the Memory Limit Directive”Insert the following line:
php_value memory_limit 256M- Replace
256Mwith the desired memory limit (e.g.,512M). - Use
Mfor megabytes andGfor gigabytes (e.g.,1G).
Step 4: Save and Verify
Section titled “Step 4: Save and Verify”-
Save the
.htaccessfile. -
Create a test file
info.phpwith:<?php phpinfo(); ?> -
Load it in your browser and search for
memory_limit. -
Confirm the new value is active.
-
Delete the test file afterward for security.
Best Practices
Section titled “Best Practices”- Increase memory gradually (e.g., 256M → 512M).
- Avoid setting unlimited memory (
-1) unless absolutely necessary. - Monitor application performance after changes.