Redirecting HTTP to HTTPS can be done in one simple step.
But that would also redirect the certbot renewals via HTTP authentication (not sure which method you use - but for that sake of all readers I will show how to redirect all except the challenge requests).
If you have a working “defualt” HTTP vhost config file, simply add this location section into it:
#set the default action for all other requests
<LocationMatch "^/(?!\.well-known)">
#send all other requests to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1
</LocationMatch>
This presumes you know how to:
[again for all readers]
edit a file in Ubuntu Linux
have enabled the Apache rewrite module
restart the web service
understand the code provided
(depending on the Apache version) you may have to also wrap the default document root in this file within a location / section
If you don’t have a working “default” HTTP vhost config file, let me know.
Feel free to show your modified file to confirm its’ usability.
This is my file
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/mydomain.com/public
<Directory /var/www/mydomain.com/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
RewriteBase /var/www/mydomain.com/public
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
#set the default action for all other requests
<LocationMatch "^/(?!\.well-known)">
#send all other requests to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1
</LocationMatch>
If you want to redirect HTTP://IP to HTTPS://IP
You will create a problem; as the certificate will NOT cover the IP (as a name on the cert).
Only HTTPS://REAL.NAMES are allowed for LE certs.
So the browser will warn you about the name mismatch [or some (more secure) browsers may block this type of access completely].
Hello, Im having other proble… When I put the URL without www, mydomain.com I have that same problem that IP, https://mydomain/var/www/mydomain.com/public