Changing PHP settings with .htaccess

Learn how to change PHP settings such as error messages, upload size, session timeout and memory limit with an .htaccess file on your web hosting.

You can adjust a wide range of PHP's default settings with a .htaccess file.

The file is placed in the folder where you want the changes to apply. The changes apply to all files and subfolders. Note that the file must be named ".htaccess" and if it does not already exist, you must create it yourself using, for example, an FTP program.

You can see a list of all PHP settings here: http://www.php.net/manual/en/ini.list.php

Some settings can also be set directly in the PHP code, using the ini_set() function. Further details about settings etc. can be found in the PHP manual: http://php.net/manual/en/ini.list.php

You can test whether the changes have been set correctly by looking at phpinfo(). Note that it is the "Local value" column that applies here.

The settings you want to change simply need to be on one line in the .htaccess file. Some popular settings are:

Hide error messages

php_flag display_errors off

Increase session lifetime (timeout)

php_value session.gc_maxlifetime 3600

Increase max upload size

php_value upload_max_filesize 512M
php_value post_max_size 512M

Increase max execution time

php_value max_execution_time 600

Increase max input vars

php_value max_input_vars 20000

Disable always populate raw post data

php_value always_populate_raw_post_data -1

Turn off OPcache

We do NOT recommend turning off opcache, as it can make your website slow.

php_flag opcache.enable off

OPcache expiry

php_value opcache.revalidate_freq 60

Memory limit

memory_limit is set to 2048MB by default for all webhosting plans, but ultimately you can never use more memory than what comes with your chosen webhosting plan.

You can set memory_limit yourself. If you need more memory, you should investigate whether the cause of the memory problem is not an error in your code, database, etc., and not just because memory_limit is 'too low' - as this is often not the case.

memory_limit can be adjusted freely through .htaccess or directly in the PHP code with ini_set().

Some CMSs (e.g. WordPress) set a memory_limit themselves, and memory_limit must then be set in a particular way. See the documentation for the CMS you are using.

Article from the support category: PHP