Redirecting a bare domain (example.com) to its www hostname (www.example.com) enforces a single canonical URL, preventing duplicate content paths, split analytics, and conflicting caches.
In Apache, the requested hostname arrives in the Host header and is handled by a matching VirtualHost, where redirect logic can be applied globally in the VirtualHost configuration or locally via .htaccess. Host-based redirects are commonly implemented with mod_rewrite so requests for the non-www hostname are consistently redirected to the www hostname while preserving the path.
DNS must resolve both hostnames to the server for redirects to work, and HTTPS introduces an extra constraint: the TLS certificate must cover both hostnames or the browser fails before any redirect can run. Misconfigured rules can create redirect loops, and 301 redirects are often cached aggressively by browsers and proxies, making controlled testing (302 first) a safer path.
Related: How to test your Apache configuration
Related: How to enable or disable Apache modules
Related: How to manage the Apache web server service
Methods to redirect domain and URL in Apache:
Redirect naked domain to www in Apache using htaccess
Using .htaccess keeps the redirect rules inside the website tree, making it practical on hosting platforms where the main Apache configuration is not writable. The redirect is evaluated per request in the directory where the request lands, so it can be applied without restructuring VirtualHost files.
The tradeoff is overhead: Apache checks for .htaccess files as requests traverse directories, which is slower than VirtualHost rules for busy sites. .htaccess rules also require AllowOverride permission in the main configuration, otherwise the file is ignored.
- Enable mod_rewrite for .htaccess-based redirects.
$ sudo a2enmod rewrite Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
- a2enmod is available on Debian and Ubuntu, and on some SUSE builds.
- RHEL-family systems commonly ship mod_rewrite enabled; confirm a LoadModule rewrite_module line exists in /etc/httpd/conf.modules.d/ if redirects do not work.
Platform How to enable mod_rewrite Debian, Ubuntu sudo a2enmod rewrite openSUSE, SLES sudo a2enmod rewrite (when available) Fedora, CentOS, RHEL Ensure LoadModule rewrite_module modules/mod_rewrite.so is present - Open the site VirtualHost configuration that controls the DocumentRoot directory.
$ sudo vi /etc/apache2/sites-available/000-default.conf
- Allow .htaccess rewrite directives by setting AllowOverride FileInfo for the site directory.
<Directory /var/www/html> AllowOverride FileInfo </Directory>
Enabling AllowOverride increases per-request overhead; prefer the VirtualHost method for high-traffic sites.
If the error log mentions FollowSymLinks or SymLinksIfOwnerMatch, ensure the directory has Options FollowSymLinks enabled.
- Restart Apache to apply module and configuration changes.
$ sudo systemctl restart apache2
- Open or create the site .htaccess file in the DocumentRoot directory.
$ sudo vi /var/www/html/.htaccess
- Add a host-based rewrite rule that redirects only the bare hostname to www.
RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Replace example.com and www.example.com with the real hostnames for the site.
R=301 is a permanent redirect; use R=302 while testing to avoid cached results.
A wrong RewriteCond can create an infinite redirect loop (ERR_TOO_MANY_REDIRECTS).
- Save the .htaccess file.
- Verify the redirect response headers from the bare domain.
$ curl -I http://example.com/docs/page.html?ref=1 HTTP/1.1 301 Moved Permanently Date: Sat, 13 Dec 2025 00:00:00 GMT Server: Apache/2.4.58 (Ubuntu) Location: https://www.example.com/docs/page.html?ref=1 Content-Type: text/html; charset=iso-8859-1
Redirect naked domain to www in Apache using Virtualhost
Redirecting in a VirtualHost avoids the per-request .htaccess lookup cost and keeps canonicalization centralized in the server configuration. This method is typically preferred for dedicated servers or VPS deployments where the Apache configuration is fully controlled.
Because VirtualHost changes are loaded during restart or reload, misconfiguration can affect all requests handled by that host. A configuration syntax test before restarting reduces the risk of taking the site offline.
- Enable mod_rewrite for VirtualHost rewrite directives.
$ sudo a2enmod rewrite Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
- This step is unnecessary when mod_rewrite is already enabled.
- A simple Redirect permanent directive can also handle this case without mod_rewrite, but rewrite rules provide more control for complex matching.
Platform How to enable mod_rewrite Debian, Ubuntu sudo a2enmod rewrite openSUSE, SLES sudo a2enmod rewrite (when available) Fedora, CentOS, RHEL Ensure LoadModule rewrite_module modules/mod_rewrite.so is present - Open the target VirtualHost configuration file in the Apache sites directory.
$ sudo vi /etc/apache2/sites-available/000-default.conf
- Configure the VirtualHost to match the www hostname plus the bare hostname via ServerAlias.
<VirtualHost *:80> ServerName www.example.com ServerAlias example.com RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L] DocumentRoot /var/www/html </VirtualHost>
Place the rewrite rules near the top of the matching VirtualHost block.
For HTTPS redirects from https://example.com, a separate :443 VirtualHost is required plus a certificate valid for both hostnames.
- Test the Apache configuration syntax before restarting.
$ sudo apachectl -t Syntax OK
- Restart Apache to apply the VirtualHost changes.
$ sudo systemctl restart apache2
- Verify the redirect response headers from the bare domain.
$ curl -I http://example.com/ HTTP/1.1 301 Moved Permanently Date: Sat, 13 Dec 2025 00:00:00 GMT Server: Apache/2.4.58 (Ubuntu) Location: https://www.example.com/ Content-Type: text/html; charset=iso-8859-1
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.
