About apache config generator
Apache uses virtual hosts to serve multiple sites and .htaccess for per-directory rules such as redirects.
Common tasks
- Virtual host for a domain.
- Reverse proxy to an application.
- Redirect http to https and set cache headers.
Test first
Run apachectl configtest before reloading Apache.
A worked example
A virtual host for example.com sets ServerName, the DocumentRoot /var/www/example and a directory block that allows .htaccess overrides. A rewrite rule can send all HTTP requests to HTTPS. After editing, run apachectl configtest and reload.
Tips
- Enable the modules you need, such as mod_rewrite and mod_proxy.
- Keep .htaccess rules few, since Apache reads it on every request.
- Disable directory listings unless you want them.
A worked example
To force HTTPS, a rewrite rule matches requests that are not secure and redirects them with a 301 to the https version of the same address. Test with curl -I http://example.com to confirm the redirect and the status code.
