Redirect people after SSL is set up
how do I set up the auto redirect with apache2. I have got SSL working and want to redirect all to the SSL side. I added Redirect / https://fraffel.tech/ but takes me to the wrong site directory whereas https takes me to the right spot. Is that the right thing to add for the redirect? (its in VirtualHost *:80)
Current VirtualHost file:
ServerName fraffel.tech
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/fraffeltech
Redirect / https://fraffel.tech/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/fraffeltech
SSLEngine on
SSLCertificateFile /etc/ssl/fraffel_tech.crt
SSLCertificateKeyFile /etc/ssl/private/fraffel.tech.key
SSLCertificateChainFile /etc/ssl/fraffel_tech.ca-bundle
</VirtualHost>
The issue here is that the directive ServerName
is missing within the <VirtualHost>
tags. This is necessary to identify the virtual host.
Also, for this case, it is a good idea to use the directive Redirect
with the option permanent
- read the section "Redirect Methods" in this article.
ServerName example.com # This directive provides a global server name.
# But you should set ServerName also for each virtual host
# to identify it!!!
<VirtualHost *:80>
ServerName example.com
# Redirect Requests to HTTPS with HTTP 301 status
Redirect permanent "/" "https://example.com/"
# Other configuration directives...
</VirtualHost>
<VirtualHost _default_:443>
ServerName example.com
# Other configuration directives...
</VirtualHost>
Apply the configuration change and restart (or reload) Apache. Then flush your browser's cache or use an incognito window (or another browser) to see the change.
For this task you can use also rewrite rules as it is illustrated in this question, but within the Apaches documentation you will be advised to do not use the rewrite engine when you can use more simple directives.