Infrastructure & Networking
What is Apache?
Apache HTTP Server, commonly called Apache, is an open-source web server software project maintained by the Apache Software Foundation that has been one of the most widely deployed web servers on the internet since its initial release in 1995. For over two decades Apache was the dominant web server, powering the majority of websites on the internet and establishing the foundational patterns of web server configuration, module architecture, and .htaccess directory-level configuration that shaped web hosting for a generation.
Apache’s full name, Apache HTTP Server, distinguishes it from other Apache Software Foundation projects. The name Apache is commonly attributed to a play on words, a patchy server, reflecting its origins as a collection of patches applied to the NCSA HTTPd server. Whatever its etymology Apache’s longevity and widespread adoption have made it a cornerstone of web infrastructure, the A in the LAMP stack, Linux, Apache, MySQL, PHP, that powered the web application explosion of the 2000s.
Apache uses a modular architecture, functionality is implemented as loadable modules that extend the core server. mod_rewrite provides URL rewriting and redirect capabilities. mod_ssl provides HTTPS and TLS support. mod_proxy provides reverse proxy capabilities. mod_cache provides HTTP caching. Modules can be enabled or disabled, allowing Apache deployments to include only the functionality needed for a specific use case.
The most significant Apache characteristic for redirect management is .htaccess support, directory-level configuration files that allow users without server administrator access to configure redirects, URL rewriting, authentication, and other settings..htaccess files made redirect configuration accessible to millions of shared hosting users, establishing Apache as the environment where many web practitioners first learned redirect management.
Apache architecture
Apache’s architecture differs fundamentally from Nginx‘s event-driven approach, using a process or thread-based model that handles each connection through a dedicated process or thread.
Multi-Processing Modules, MPMs: Apache’s connection handling architecture is implemented through Multi-Processing Modules, MPMs, that determine how Apache accepts connections and processes requests. The primary MPMs are:
Prefork MPM: the original Apache process model. A pre-forked pool of single-threaded worker processes each handles one connection at a time. The master process monitors the worker pool, spawning new workers when demand increases and killing idle workers when demand decreases. Prefork is the safest MPM for compatibility with non-thread-safe PHP extensions and other non-thread-safe modules, each worker has independent memory space. However Prefork’s memory consumption scales linearly with concurrent connections, each worker process requires significant memory regardless of whether it is actively processing a request.
Worker MPM: a hybrid process-threaded model. Multiple processes each run multiple threads, threads handle individual connections. Worker uses less memory than Prefork, multiple connections share a single process’s memory overhead, while maintaining process isolation between process boundaries. Worker requires thread-safe modules, not compatible with non-thread-safe PHP extensions.
Event MPM: the modern Apache MPM designed for high concurrency. Like Worker it uses multiple processes with multiple threads, but uses an event-driven mechanism for connection management. Persistent connections, keep-alive, are handled by a dedicated connection management thread rather than occupying a full worker thread. This allows worker threads to handle active requests efficiently while the connection manager maintains idle keep-alive connections. Event MPM is the recommended MPM for modern Apache deployments.
Configuration file structure: Apache configuration is defined in a hierarchical file structure. The main configuration file, httpd.conf or apache2.conf: contains global settings. VirtualHost blocks define per-domain configuration. Directory blocks define per-directory configuration. Include directives reference additional configuration files. On Debian-based Linux distributions, Ubuntu, Debian, Apache configuration is split across multiple files in /etc/apache2/: apache2.conf for global settings, sites-available/ for virtual host configurations, mods-available/ for module configurations.
Apache virtual hosts
Apache serves multiple websites on a single server through virtual hosts, server blocks that define configuration for specific domains.
Name-based virtual hosts: the standard virtual host configuration using the HTTP Host header to distinguish between sites on the same IP address and port.
The ServerName directive specifies the primary domain. ServerAlias specifies additional domain names for the same virtual host. DocumentRoot specifies the filesystem directory from which Apache serves content. *:80 listens on all IP addresses on port 80, *:443 listens on all IP addresses on port 443.
Redirect-only virtual hosts: virtual hosts that serve no content, only redirect all traffic to another domain:
The HTTPS virtual host for the old domain requires a valid SSL certificate, HTTPS requests must complete the TLS handshake before the redirect response can be sent.
Apache mod_rewrite
Apache’s mod_rewrite module provides comprehensive URL rewriting and redirect capabilities, the most powerful and flexible redirect mechanism in Apache.
Enabling mod_rewrite: mod_rewrite must be enabled before use. On Ubuntu and Debian:
In virtual host or directory configuration:
RewriteRule syntax: the core mod_rewrite directive:
Pattern is a regular expression matched against the URL path, without the leading slash by default in.htaccess context. Substitution is the replacement URL, can include captured groups from the pattern. Flags modify rule behaviour, enclosed in square brackets.
Basic redirect examples:
RewriteCond, conditions: conditions that must be satisfied before the following RewriteRule fires:
RewriteMap, database-driven redirects: mod_rewrite supports external lookup maps, files or databases mapping source URLs to redirect destinations. RewriteMap enables large-scale redirect management beyond what is practical with individual RewriteRule directives:
The map file redirect-map.txt contains source-destination pairs:
RewriteMap lookups are efficient, Apache loads the map into memory for fast lookups, making this approach practical for hundreds or thousands of redirect rules.
Apache SSL configuration
Apache handles SSL/TLS through mod_ssl, providing HTTPS termination and certificate management.
Basic SSL configuration:
Let’s Encrypt with Certbot: Certbot supports Apache with automatic certificate provisioning and configuration:
Certbot modifies the Apache virtual host configuration to include SSL directives and certificate paths, creating a new SSL virtual host if one does not exist. Automatic renewal is configured through a cron job or systemd timer.
Apache as a reverse proxy
Apache’s mod_proxy module provides reverse proxy capabilities, forwarding requests to backend application servers.
Basic reverse proxy configuration:
ProxyPass forwards requests to the backend. ProxyPassReverse rewrites Location headers in redirect responses from the backend, ensuring redirects from the backend use the correct frontend URL rather than the backend URL. ProxyPreserveHost forwards the original Host header to the backend.
Load balancing with mod_proxy_balancer:
lbmethod=byrequests distributes requests round-robin. lbmethod=bytraffic distributes by bytes transferred. lbmethod=bybusyness routes to the least busy worker.
Apache vs Nginx for redirect management
The choice between Apache and Nginx for redirect infrastructure depends on several factors.
Shared hosting compatibility: Apache with.htaccess is the only option on shared hosting environments where server configuration access is not available. Nginx does not support.htaccess, requiring server-level configuration access for all redirect changes. For shared hosting environments Apache is the only practical choice.
Performance at scale: Nginx handles high-concurrency workloads with lower memory consumption than Apache’s Prefork MPM. For high-traffic redirect infrastructure, serving millions of redirects per day, Nginx’s event-driven architecture provides better performance. Apache’s Event MPM closes much of this performance gap for modern deployments.
Configuration complexity: both Apache mod_rewrite and Nginx rewrite syntax have learning curves. Apache’s mod_rewrite is older and has more community documentation. Nginx’s return directive is simpler for common redirect patterns. Complex pattern-based redirects are roughly equivalent in complexity between the two.
Ecosystem and tooling: many hosting control panels, cPanel, Plesk, provide GUI interfaces for managing Apache redirect rules, reading and writing.htaccess files. This tooling makes Apache redirect management accessible to non-technical users on managed hosting. Nginx lacks equivalent hosting panel integration, redirect management typically requires direct configuration file editing.