GZIP with .htaccess
Reviewedbyfl
Markdown ↓Compress responses for faster delivery.
GZIP compression reduces file size for faster delivery. fortrabbit automatically compresses text documents, but you can extend this to other content types like JSON APIs.
HTTP responses are automatically compressed if the content-type header indicates a text document, such as text/css or text/html. To enable GZIP compression for other HTTP responses, such as REST or GraphQL APIs, use the AddOutputFilterByType directive:
AddOutputFilterByType DEFLATE application/json
1. Make sure environment variables are set
In the dashboard with your fortrabbit environment, make sure an ENV var key-value pair like this is present:
ENVIRONMENT=production
In some cases we have pre-populated that for you already. See also our environment variables topic for more details on how environment variables are handled here.
2. Setup .htaccess files
Now in the local code base of your project, setup a custom .htaccess file for each environment in the web root of your project:
.htaccessin general and for local development.htaccess_productionfor your fortrabbit app
This can also be extended to more environments. All .htaccess files should be under version control with Git. For an introduction to .htaccess configuration, see .htaccess basics.
3. Configure Composer
Add this to the script part of your composer.json in your local code base:
"scripts": {
"post-install-cmd": [
"@php -r \" define('HTSRC', 'web/.htaccess_' . getenv('ENVIRONMENT')); echo (file_exists(HTSRC) && copy(HTSRC, 'web/.htaccess')) ? HTSRC.' copied to web/.htaccess'.PHP_EOL : ''; \""
]
}
This Composer post install command will replace the contents of the .htaccess file with the contents of the file .htaccess_production, if that file is present and the environment variable ENVIRONMENT is set to production.
You can extend this approach to support more environments. This example assumes that the root path of your app is web. In the Composer example above, replace web with the root path of your project.