Jump to content

Recommended Posts

LetsEncrypt forces "https" by adding these rules to /apache2/sites-enabled/000-default.conf

<VirtualHost *:80>
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Question:
What is the "best practices way" to ALSO have "non-www" redirect to "www" (with the least possible redirects)?

Concerns:
Although http redirects to https just fine, I cannot get https://example.com to redirect to https://www.example.com properly.

Thank you.

1 hour ago, requinix said:

If you can't get it to work then that means you tried something.

Absolutely. I think I spent over a week experimenting!

1 hour ago, requinix said:

What did you try

Almost every suggestion on the net that I could find, maybe hundreds of lines of code.

 

1 hour ago, requinix said:

what happened when you tested to see if it worked?

Usually multiple redirects. Instead of one or two redirects, sometimes 3 or 4.

Are you familiar with the free https service "LetsEncrypt?" If you have that on your server(s), what is your configuration?

14 minutes ago, StevenOliver said:

Almost every suggestion on the net that I could find, maybe hundreds of lines of code.

Pick the one that looks most reputable and looks like what I'm about to describe, try it again, and if that still doesn't work then post exactly what you did.

14 minutes ago, StevenOliver said:

Usually multiple redirects. Instead of one or two redirects, sometimes 3 or 4.

There should only be one redirect: if the URL isn't www or it isn't HTTPS then redirect to the HTTPS+www URL.

14 minutes ago, StevenOliver said:

Are you familiar with the free https service "LetsEncrypt?" If you have that on your server(s), what is your configuration?

image.png

We use them for the certificate but handle the server configuration ourselves.

Discovered something:
LetsEncrypt installs All the "http -> https" rules in a special conf file under "<VirtualHost *:80>"
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

But, if the URL already begins with https, then those rules don't get triggered (because those rules are under VirtualHost listening at http port 80).

I noticed LetsEncrypt installs another file with <VirtualHost *:443>.

Placing "non-www --> www" Rewrite Rules under that VirtualHost 443 heading now makes all my URLs look correct.

HOWEVER.....

I TRIED SOMETHING ELSE: Having some RewriteRules in one file under VirtualHost 80, and some RewriteRules in another file under VirtualHost 443 irritates me. So I deleted them and added the following code at the bottom of my apache2.conf file:
<Directory /var/www/html/>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
</Directory>

QUESTION: EVEN THOUGH THIS SEEMS TO WORK, IS THIS OKAY TO DO? ANY PROBLEMS WITH THIS?

Edited by StevenOliver

 

10 hours ago, requinix said:

Perhaps you would like to be so much like an AI bot it's funny. me made your day?

I am glad you made day. I like funny too. My favorite color is green. What's your favorite color? I like macaroni and cheese.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.