Redirect http to https

Hello, I created a certificate without redirecting http to https, now I need to redirect it and I don’t know how.

The operating system my web server runs on is (include version): Ubuntu 20 / Apache2

My hosting provider, if applicable, is: Digital Ocean

The version of my client is (e.g. output of certbot --version or certbot-auto --version if you’re using Certbot): Last version

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.

1 Like

Its working!

Thank you!

1 Like

Cheers from Miami :beers:

[mark this solved]

1 Like

Its working good, but now, when I put the IP at URL redirect me to:

https://157.245.138.127/var/www/mydomain.com/public

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>

RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]
RewriteCond %{SERVER_NAME} =mydomain.com.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

And you want to access your site via IP and HTTP?

And you already had this code in there?:

No, only http, but I dont know if that is a problem

Yes, Maybe it was generated when I tried to reinstall the certificate and changed the option to redirect

If you only want to redirect when it has your domain name (and not when it is an IP) then
replace:

with

and then delete:

Thank you, and if I want redirect IP too, I must only remove the last line?

RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

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].

1 Like

The last line is redundant to the other rewrite rule above it and is unreachable code - that will never happen twice.

Okey, Thank you again!

1 Like

Yes, Im learning. Thank you!

1 Like

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

I have that

<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 %{SERVER_NAME} =www.mydomain.com [OR]
      RewriteCond %{SERVER_NAME} =mydomain.com
      RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1
    </LocationMatch>

The problem is…
You want to always see “www” in the https?

Why is that code there?