StevenOliver Posted June 30, 2020 Share Posted June 30, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/ Share on other sites More sharing options...
requinix Posted June 30, 2020 Share Posted June 30, 2020 If you can't get it to work then that means you tried something. What did you try and what happened when you tested to see if it worked? Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/#findComment-1579293 Share on other sites More sharing options...
StevenOliver Posted June 30, 2020 Author Share Posted June 30, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/#findComment-1579302 Share on other sites More sharing options...
requinix Posted June 30, 2020 Share Posted June 30, 2020 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? We use them for the certificate but handle the server configuration ourselves. Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/#findComment-1579304 Share on other sites More sharing options...
StevenOliver Posted June 30, 2020 Author Share Posted June 30, 2020 (edited) 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 June 30, 2020 by StevenOliver Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/#findComment-1579309 Share on other sites More sharing options...
requinix Posted June 30, 2020 Share Posted June 30, 2020 2 hours ago, StevenOliver said: QUESTION: EVEN THOUGH THIS SEEMS TO WORK, IS THIS OKAY TO DO? ANY PROBLEMS WITH THIS? That's pretty much exactly what I said you needed to do, so... 1 Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/#findComment-1579312 Share on other sites More sharing options...
StevenOliver Posted July 1, 2020 Author Share Posted July 1, 2020 (edited) OMG you are so much like an AI bot it's funny. You made my day!! 😀 (check this out: https://www.pandorabots.com/mitsuku/) Edited July 1, 2020 by StevenOliver Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/#findComment-1579313 Share on other sites More sharing options...
requinix Posted July 1, 2020 Share Posted July 1, 2020 Perhaps you would like to be so much like an AI bot it's funny. me made your day? 1 Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/#findComment-1579314 Share on other sites More sharing options...
StevenOliver Posted July 1, 2020 Author Share Posted July 1, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311021-redirect-non-www-to-www-still-not-working/#findComment-1579328 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.