How to I tell Apache to redirect HTTP to HTTPS after setting up Let's Encrypt

After installing Let’s Encrypt on Ubuntu with Apache for a WordPress site, Let’s Encrypt asked to select to add a redirect (option 2) to what I said yes, because this was for an existing site that was on HTTP.

Then checking the 000-default.conf file these lines where added:

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

My question: Is above lines the same and these other lines below:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I’m asking because, I’ve found this interesting article from Yoast.com, known for its SEO plugin for WordPress: https://yoast.com/moving-your-website-to-https-ssl-tips-tricks/ and it recommends to add a 301 redirect in default VirtualHost configuration to properly switch to from HTTP to HTTPS.

Also, long time ago I had some weird URL structure and later on I switch to example.com/post-name structure to make the switch I had to add this redirect line to my htaccess files:

RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(?!page/)(.+)$ http://example.com/$4

The question is: after adding the correct HTTP to HTTPS redirect to the VirtualHost file will I have any conflicts with the RedirectMatch 301 mentioned above?

PS: I’m aware that in the RedirectMatch 301 I’ll have to change the http:// to https:// after installing Let’s Encrypt.

They both do the same thing, more or less. R=301 and R=permanent are equivalent. The 301 status code is defined as “Moved Permanently”, hence the alias. The only real difference is the second one won’t redirect if it is placed in an HTTPS virtual host, so it is suitable to be placed in .htaccess files. But since certbot knows it is editing a non-HTTPS virtual host, it does it a bit differently.

Your RedirectMatch should be fine. It would work even if you didn’t change it to https since it would redirect to http then back to https at the correct URL. Changing it will avoid the double-redirect and make pages load faster, however.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.