.htaccess intro
Published
Updated
Apache web server configuration via .htaccess.
fortrabbit runs the Apache 2.4 web server, and Apache reads `.htaccess` files in the code. Use them for redirects, access rules, compression and HTTP headers. The Apache configuration itself is managed by fortrabbit.
Where the file goes
A .htaccess file applies to its own folder and every folder below it. The main one sits in the root path of the environment, for example public for Laravel or web for Craft CMS. Commit it with the code and it deploys with every git push.
The leading dot makes the file hidden in most file managers. A missing .htaccess is a common cause of 404 errors, see troubleshooting.
What the platform sets
| Setting | On fortrabbit |
|---|---|
| HTTP to HTTPS | Every request on http:// gets a 301 redirect to https://, no rule needed |
| Compression | Off until .htaccess enables it, see GZIP and Brotli |
.ht* and .user.ini files | Never served |
| Visitor IP | REMOTE_ADDR holds the visitor's address, not the load balancer's |
| Error pages | fortrabbit error pages for 4xx and 5xx, replaceable with ErrorDocument |
| Access syntax | Apache 2.4 Require and the older 2.2 Order, Allow, Deny both work |
Allowed directives
The Apache setting AllowOverride decides which directives a .htaccess file may use. On fortrabbit:
| Group | Examples | Allowed |
|---|---|---|
FileInfo | RewriteRule, Header, ErrorDocument, AddOutputFilterByType, SetEnvIf | Yes |
Limit | Order, Allow, Deny | Yes |
Indexes | DirectoryIndex, IndexOptions | Yes |
Options | MultiViews, Indexes, SymLinksIfOwnerMatch only | Partly |
AuthConfig | AuthType, AuthUserFile, Require | Only while the dashboard password protection is off |
A directive outside these groups, or one that only works in the server configuration such as <Location>, ends in a 500 Internal Server Error for every request below that folder. The Apache error shows up in the logs.
Framework and CMS defaults
Most frameworks and CMS ship a .htaccess that routes requests to index.php. Extend it instead of replacing it:
- Laravel:
public/.htaccess - Craft CMS:
web/.htaccess - WordPress: writes its own rules into
.htaccess, see WordPress and httpd