0

This is probably a duplicate but I can't find quite what I'm looking for. I'd like to redirect http and/or non-www to https+www. This is often cited as a canonical example of when to avoid mod_rewrite.

My current setup works, using only mod_alias' Redirect, but requires an extra VirtualHost with identical SSL configuration for one case.

Is there any way to simplify this using Redirect only? Or would that require mod_rewrite? Thanks!

# Redirect http with or without www
<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  Redirect / https://www.example.com/
</VirtualHost>

# Redirect https without www
<VirtualHost *:443>
  ServerName example.com
  Redirect / https://www.example.com/
  SSLCertificateFile /etc/ssl/example/cert.pem
  SSLCertificateKeyFile /etc/ssl/example/key.pem
</VirtualHost>

# Final destination
<VirtualHost *:443>
  ServerName www.example.com
  DocumentRoot /var/www/example
  SSLCertificateFile /etc/ssl/example/cert.pem
  SSLCertificateKeyFile /etc/ssl/example/key.pem
  ...
</VirtualHost>
Toni
  • 154
  • 5
Rob
  • 1
  • 1

1 Answers1

0

For posterity, I discovered mod_macro. It gets rid of a lot of duplication by letting me write a Macro template for the blocks above and reducing my actual VirtualHost files to a single Use line with variables. Look for examples in the Apache docs.

Rob
  • 1
  • 1