Server

Redirecting www to non-www (or the other way) with .htaccess

HCOMS March 2026 4 min read

You uploaded a 60MB image to WordPress and it died with "Allowed memory size of 134217728 bytes exhausted". Or your Laravel artisan command ran out of memory mid-import. This is the most common cause: PHP's default memory_limit is set conservatively, and your script wants more.

There are four places you can raise it, in descending order of how much access you need to your server.

1. php.ini (best, if you can)

If you have shell access to your server, find the active php.ini:

php --ini

Edit it and set:

memory_limit = 512M

Then restart PHP-FPM (sudo systemctl restart php8.3-fpm) or Apache (sudo systemctl restart apache2). Verify:

php -i | grep memory_limit

2. .user.ini (shared hosting friendly)

On most shared hosting that runs PHP via FastCGI/PHP-FPM, you can drop a file called .user.ini in your site's public root with just:

memory_limit = 512M

The setting takes effect within five minutes (PHP re-reads .user.ini files periodically). No restart required, no SSH access needed. This is the right answer for cPanel and similar hosts.

3. .htaccess (only when PHP is loaded as an Apache module)

php_value memory_limit 512M

This only works if your host runs PHP as mod_php. On any modern host using PHP-FPM or FastCGI it will throw a 500 Internal Server Error the moment a request hits .htaccess. If you're not sure which your host uses, try .user.ini first.

4. ini_set() in your code (last resort)

<?php
ini_set('memory_limit', '512M');

This works at runtime but only for the script that called it. It's useful for one-off CLI scripts (php -d memory_limit=2G import.php) and absolutely the wrong place for it on a production web app.

How much should you give it?

For a typical CMS-backed site, 256M is generous. For Laravel apps doing image manipulation or PDF generation, 512M is reasonable. If you find yourself reaching for 2G, the problem isn't memory — it's that you're loading the entire database into a single array somewhere. Refactor instead.

WordPress specifically

WordPress also reads its own constant in wp-config.php:

define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');

The first applies to the front end, the second to admin. Both are silently capped by the underlying PHP memory_limit, so this is in addition to one of the four methods above, not instead of.

If your site is regularly hitting the limit, that's worth investigating — it usually means a runaway plugin or an unbounded query. Happy to take a look if you want a second pair of eyes.

Related notes