On this page
- Why should a site answer on only one address?
- How do you get one certificate for both names?
- How do you redirect HTTP to HTTPS in Nginx?
- How do you send www to the bare domain over HTTPS in Nginx?
- How do you set up the same redirects in Apache?
- How do you add HSTS safely?
- How do you test every combination?
- What goes wrong most often?
- Questions
Why should a site answer on only one address?
A single canonical address keeps search engines, caches, cookies and analytics pointed at one place. Without redirects, http://example.net, http://www.example.net, https://example.net and https://www.example.net can all serve the same page, which splits ranking signals and lets cookies set on one name go missing on another. Plain HTTP also exposes form data and session cookies to anyone on the network path.
Choose the form you prefer once and stick with it. The bare domain is shorter to type; the www form makes it easier to set cookies that do not leak to other subdomains. Both work. This guide uses https://example.net as the target, and the reverse choice only means swapping the names in each block.
Before you start, both names need DNS records pointing at the server (198.51.100.24 and 2001:db8:4f2a::24), as set up in the DNS records tutorial, and ports 80 and 443 need to be open in the OS firewall.
How do you get one certificate for both names?
Request a single certificate with both names on it, because a redirect from https://www.example.net still needs a valid certificate for www before the browser will follow it. Certbot handles this with two -d flags:
# Ubuntu and Debian
sudo apt install -y certbot python3-certbot-nginx # or python3-certbot-apache
# Rocky Linux 9 and AlmaLinux 9
sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-nginx # or python3-certbot-apache
sudo certbot certonly --nginx -d example.net -d www.example.net
sudo certbot certificatesUsing certonly fetches the files without letting Certbot rewrite your configuration, so you stay in control of the redirect logic written below. The files land in /etc/letsencrypt/live/example.net/. For Apache, replace --nginx with --apache. The Certbot guide covers renewal hooks and wildcard names in more depth.
How do you redirect HTTP to HTTPS in Nginx?
Give port 80 its own small server block that does nothing except return a permanent redirect. Keeping it separate is cleaner than an if test inside the main block, and Nginx documentation itself recommends return over rewrite for whole-site moves.
server {
listen 80;
listen [::]:80;
server_name example.net www.example.net;
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt;
}
location / {
return 301 https://example.net$request_uri;
}
}The $request_uri variable carries the original path and query string, so http://www.example.net/shop?id=7 lands on https://example.net/shop?id=7 in one hop. The ACME location is optional; it lets webroot renewals keep working over HTTP. Create that directory with sudo mkdir -p /var/www/letsencrypt if you keep it.
How do you send www to the bare domain over HTTPS in Nginx?
Add a second port 443 block that answers only for www.example.net, presents the same certificate and redirects. The main block then serves content for the bare name alone.
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name www.example.net;
ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
return 301 https://example.net$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.net;
ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
root /var/www/example.net/public;
index index.html index.php;
}The http2 on; directive needs Nginx 1.25.1 or newer. Ubuntu 24.04 ships 1.24 and Debian 12 ships 1.22, so on those versions drop that line and write listen 443 ssl http2; instead. Check yours with nginx -v, then validate and apply with sudo nginx -t && sudo systemctl reload nginx. To prefer www instead, swap the two server_name values and point the redirect at https://www.example.net$request_uri.
How do you set up the same redirects in Apache?
In Apache, the port 80 virtual host can use a one-line Redirect permanent, which needs no extra module. The www host on port 443 gets its own virtual host that does the same. On Debian and Ubuntu the files live in /etc/apache2/sites-available/; on Rocky and Alma put them in /etc/httpd/conf.d/example.net.conf and install mod_ssl first with sudo dnf install -y mod_ssl.
<VirtualHost *:80>
ServerName example.net
ServerAlias www.example.net
Redirect permanent / https://example.net/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.net
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.net/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.net/privkey.pem
Redirect permanent / https://example.net/
</VirtualHost>
<VirtualHost *:443>
ServerName example.net
DocumentRoot /var/www/example.net/public
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.net/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.net/privkey.pem
</VirtualHost>Redirect keeps everything after the matched prefix, so deep links survive. On Debian and Ubuntu enable the modules and the site, then reload:
sudo a2enmod ssl headers
sudo a2ensite example.net
sudo apache2ctl configtest && sudo systemctl reload apache2
# Rocky and Alma
sudo apachectl configtest && sudo systemctl reload httpdIf your site lives on shared-style hosting rules in .htaccess, a RewriteRule with %{HTTPS} works too, but configuring the virtual host directly is faster because Apache does not read .htaccess on every request.
How do you add HSTS safely?
Add HSTS only after the redirects work, and start with a short lifetime. The header tells browsers to use HTTPS for the domain for the given number of seconds, even if someone types http://. If HTTPS later breaks, visitors cannot click past the error, so ramp up slowly.
# Nginx, inside the port 443 server block for example.net
add_header Strict-Transport-Security "max-age=300" always;
# Apache, inside the port 443 VirtualHost for example.net
Header always set Strict-Transport-Security "max-age=300"After a day without trouble, raise the value to a week (604800), then to a year (31536000). Add includeSubDomains only when every subdomain, including old ones such as mail.example.net or internal tools, already serves valid HTTPS. The preload token asks browser vendors to hard-code your domain; removal takes months, so treat it as a one-way decision. One Nginx detail matters: an add_header inside a location block replaces all headers inherited from the server level, so repeat the HSTS line in any location that sets its own headers.
How do you test every combination?
Use curl to request each of the four variants and confirm that each one reaches the canonical address in a single step. The -I flag fetches headers only, and -L follows redirects so you can see the whole chain.
for u in http://example.net http://www.example.net https://www.example.net https://example.net; do
echo "== $u"
curl -sI "$u/test?x=1" | grep -iE "^(HTTP|location|strict)"
doneThe first three should print HTTP/1.1 301 or HTTP/2 301 with a location: https://example.net/test?x=1 line. The last one should show 200 (or 404 for this made-up path) plus the HSTS header. If you see two hops, for example http to https://www to bare, one block is redirecting to the wrong scheme or name. Browsers cache 301 responses aggressively, so test with curl or a private window while you adjust.
What goes wrong most often?
- Redirect loop: usually a proxy or app in front that already ends TLS and forwards plain HTTP, so the backend keeps redirecting. Redirect based on
X-Forwarded-Protothere, or only at the outermost server. - Certificate warning on www: the certificate lacks the www name. Run
sudo certbot certificatesand reissue with both-dflags. - Port 80 closed: HTTP-01 renewals fail. Keep port 80 open and serving the redirect; see the firewall tutorial for UFW and firewalld rules.
- Wrong default server: a request for an unknown name lands on your site. Add a catch-all block that returns
444in Nginx, or a first virtual host that serves nothing in Apache. - Application URLs: CMS settings such as the WordPress site address must match the canonical form, or the app will redirect back the other way.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about HTTPS and www redirects
Should I use 301 or 308 for the HTTPS redirect?
Both are permanent. A 301 is universally understood and fine for GET requests. A 308 also keeps the request method and body, which matters only if clients POST to the old HTTP address, such as an API or webhook sender.
Does the redirect hurt search rankings?
A single permanent redirect passes the page to its new address. Problems come from chains of several hops or from mixing 302 temporary redirects, so keep it to one 301 per request.
Can I redirect without a certificate for www?
Plain HTTP requests to www can redirect without one, but anyone who types or bookmarks https://www.example.net will hit a certificate error first. Include both names on the certificate.
Is HSTS required?
No, but it closes the gap where the very first request of a visit goes over HTTP and could be intercepted. Once the redirects are stable, a long max-age is a cheap improvement.
How do I undo HSTS if I made a mistake?
Send the header with max-age=0 over HTTPS. Each browser drops the rule the next time it visits. Preloaded domains need a separate removal request and take much longer.
Do these redirects work behind Varnish or another proxy?
Yes, as long as the redirect lives at the layer that terminates TLS. See the Varnish guide for a layout with Nginx on port 443 in front.
More in Web servers and proxies
- Install and configure Nginx
- Install and configure Apache
- OpenLiteSpeed web server
- HAProxy load balancer and TLS proxy
- Load balancing with Nginx upstreams
- Nginx as a reverse proxy for an app
- Apache as a reverse proxy
- Compression with gzip and Brotli
- Turn off Apache directory listings
- Nginx Proxy Manager with Docker
- Nginx UI web interface
- Traefik for Docker containers
- Apache Tomcat for Java apps
- Encrypted FTP with vsftpd (FTPS)
- Varnish cache in front of a site