If you’re working on your own LAMP server or setting up a site for a client, one of the small but important things you should handle is how your domain resolves. Whether someone types in example.com or www.example.com, you want them to end up in the same place, preferably the www version for consistency and SEO purposes.
<—– Video ——>
In this post, I’ll show you how to set up an Apache HTTPS VirtualHost configuration that automatically redirects any non-www requests to the www version of your site. This ensures a clean, unified domain and avoids duplicate content issues. We’ll also properly use the ServerAlias directive.
This example is from my private home office Configuration. My configuration:
-
- Windows 10 laptop.
- VirtualBox installed on Windows.
- VirtualBox is used to create LAMP (Linux, Apache, MySQL, PHP) virtual servers for PHP development and testing.
- Using the Linux and Windows hosts files as Domain Name System (DNS) for these virtual servers.
- I use non-rountable domain names. In this example I am using lamp.internal.
- All of this is on a private network utilizing a non-routable IP addresses.
Prerequisites
Before setting up the non-www to www redirect in your Apache HTTPS VirtualHost, make sure you have the following:
-
- Apache web server installed and running on your Linux server (Ubuntu or similar).
- Root or sudo access to edit Apache configuration files (usually in /etc/apache2/sites-available/).
- SSL certificate and key files installed and properly configured for your domain, covering both example.com and www.example.com.
- Basic knowledge of how to restart Apache (sudo systemctl restart apache2) after configuration changes.
- Your website’s document root directory path (e.g., /var/www/example).
Backup of your existing Apache config files before editing, to avoid issues if you need to revert.
If you don’t have an SSL certificate yet, consider using Let’s Encrypt, it’s free and easy to set up with tools like Certbot.
In this tutorial and associated video, I use a Self-Signed Certificate.
Follow this link to the article and associated video on How To Create a Self-Signed SSL Certificate for Apache in Ubuntu 24.04
.
Here’s the basic idea:
You need two VirtualHost blocks – one for example.com that handles the redirect, and one for www.example.com that actually serves your website content.
In the example.com block, we use “Redirect permanent” to send all traffic to https://www.example.com/.
In the www.example.com block, we use ServerName and ServerAlias to define the canonical domain and any aliases you might want to handle in the future.
I have two virtual host files. One for port 80/not secure and port 443/secured.
Here is the port 80 configuration:
<VirtualHost *:80>
ServerAdmin webmaster@site1.com
ServerName lamp.internal
ServerAlias www.lamp.internal
DocumentRoot /var/www/lamp.internal/public_html
#########################################
# Redirect HTTP requests to HTTPS & WWW #
#########################################
Redirect permanent / https://www.lamp.internal
<Directory /var/www/lamp.internal/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.3-fpm.lamp.sock|fcgi://localhost/"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/lamp.internal.error.log
CustomLog ${APACHE_LOG_DIR}/lamp.internal.access.log combined
</VirtualHost>
Here is the port 443 configuration:
<VirtualHost *:443>
##################################################
# ServerName and ServerAlias values are reversed #
##################################################
ServerName www.lamp.internal
ServerAlias lamp.internal
DocumentRoot /var/www/lamp.internal/public_html
###########################################
# Note ServerName and ServerAlias are #
# switched from the port 80 configuration #
###########################################
SSLEngine on
SSLCertificateFile /etc/ssl/internal/lamp.internal/lamp.internal.crt
SSLCertificateKeyFile /etc/ssl/internal/lamp.internal/lamp.internal.key
#################################
# Redirect non-www HTTPS to www #
#################################
RewriteEngine On
RewriteCond %{HTTP_HOST} ^lamp\.internal$ [NC]
RewriteRule ^(.*)$ https://www.lamp.internal$1 [L,R=301]
###############
# mod_rewrite #
###############
# .htaccess has the other needed mod_rewrite code
<Directory /var/www/lamp.internal/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
##################################################
# Start PHP-FPM #
# This is the PHP-FPM code. If you are not #
# running PHP-FPM you can comment out or remove. #
# Adjust PHP socket version if needed. #
##################################################
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.3-fpm.lamp.sock|fcgi://localhost/"
</FilesMatch>
# End PHP-FPM
ErrorLog ${APACHE_LOG_DIR}/lamp.internal.error.log
CustomLog ${APACHE_LOG_DIR}/lamp.internal.access.log combined
</VirtualHost>
Conclusion
In this article we covered how to permanently redirect your website from the non-WWW version to the WWW-version of your website. In doing so every visitor of your website will be directed to the WWW-version.
This redirect setup may seem like a small detail, but it makes a big difference. It keeps your URLs clean, helps with SEO, and gives your site a more professional feel. If you’re managing a LAMP server, getting comfortable with the Apache configs like this will give you more control and polish on every project.