0

So I want to redirect example.com to https://www.example.com. I already have ssl with certbot but I want to change my redirect config in /etc/apache2/sites-available/example.conf.

Certbot already gives you a redirect from example.com to https://example.com.

this is my current apache config

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
  • Does this answer your question? [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About mod\_rewrite Rules but Were Afraid to Ask](https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Tero Kilkanen Jan 26 '22 at 15:33
  • @TeroKilkanen Yup this answers it. Thank you. – Thamognya Kodi Jan 27 '22 at 10:38

1 Answers1

2

The recommended configuration for simple redirects is to avoid rewrite rules and simply set up a Redirect directive:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent "/" " https://www.example.com/" 
</VirtualHost>
Bob
  • 5,335
  • 5
  • 24