StevenOliver Posted January 13, 2019 Share Posted January 13, 2019 Hello. To redirect "non-www to www" and "non-https to https," I found this efficient three-liner: RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\.example\.com$ RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] I would also like to redirect /index.html to root, what's the best way? (By "best," I mean fewest redirects and least strain on Apache.) Examples: http://example.com/data.html redirects to https://www.example.com/data.html http://example.com/index.html redirects to https://www.example.com/ Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/ Share on other sites More sharing options...
requinix Posted January 14, 2019 Share Posted January 14, 2019 I kinda feel like being tricky. RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\.example\.com$ [OR] RewriteCond %{REQUEST_URI} ^/index\.html(\?|$) RewriteRule ^index\.html$|(.*) https://www.example.com/$1 [L,R=301] Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563561 Share on other sites More sharing options...
StevenOliver Posted January 18, 2019 Author Share Posted January 18, 2019 Requinix, thank you very much! That is fantastic!! I know it's a week later.... but I purposely didn't check back for a few days because I wanted to see what I could come up with all by myself :-) The goal was twofold 1. No more than one redirect. 2. Least amount of strain on Apache. Here's what I came up with: RewriteCond %{REQUEST_URI} /index\.html$ RewriteRule (.*?)(index\.html)?$ https://www.example.com$1 [NE,R=301,L] RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\.example\.com$ RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] And just now I saw your reply. My code has 5 lines. Your code has 4 lines. You win! You beat me! I'll use your code, less lines of code means less work for the Apache server. Thank you!!! Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563674 Share on other sites More sharing options...
requinix Posted January 18, 2019 Share Posted January 18, 2019 It also means one less redirect. Your method can result in two: first to remove the index.html, second to use HTTPS with the right subdomain. Also yours doesn't allow for query strings on index.html. And a slight edit to mine: RewriteRule ^/?index\.html$|/?(.*) https://www.example.com/$1 [L,R=301] Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563675 Share on other sites More sharing options...
StevenOliver Posted January 19, 2019 Author Share Posted January 19, 2019 I just discovered a problem though. The redirects work most of the time.... for example: http://example.com/merchandise.html correctly redirects tohttps://www.example.com/merchandise.html However, http://example.com/blog redirects tohttp://example.com/blog/ (just adds a trailing slash but no https) -and- Erratic results using same browser, clearing browser's cache inbetween tests: example.com/blog sometimes redirects tohttps://www.example.com (omits the subdirectory) http://example.com/blog sometimes redirects to example.com/blog (omits the 'http' entirely, doesn't add trailing slash) example.com/blog sometimes redirects to example.com/blog./ (just adds a trailing slash, but no https) hmmm.... Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563680 Share on other sites More sharing options...
StevenOliver Posted January 20, 2019 Author Share Posted January 20, 2019 edit: (update): Because I no longer need the "index.html to /", this is the current code: RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\.example\.com$ RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] However, the problem is this code forgets to force the "www" when inputting a URL like "example.com/directory" example.com/directory resolves to https://example.com/directory/ instead of the desired "https://www.example.com/directory/" Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563703 Share on other sites More sharing options...
requinix Posted January 20, 2019 Share Posted January 20, 2019 Then it must mean the rewriting isn't working anymore because that's what you had before. Have you changed anything else? Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563706 Share on other sites More sharing options...
StevenOliver Posted January 20, 2019 Author Share Posted January 20, 2019 No, nothing changed. In about an hour I'll be at my other computer, and I'll try a scientific test: I'll place the same code on 2 different webhost's servers using fresh blank .htaccess files, and newly created "test" subdirectories with a "hello world" index file in each one. Then I'll try again and see if http://example.com/testdirectory redirects to http://www.example.com/testdirectory/ I'll come back here and post the results. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563714 Share on other sites More sharing options...
StevenOliver Posted January 21, 2019 Author Share Posted January 21, 2019 No sense.... just makes no sense.... now the redirect is working. And it is working on ALL my sites. There is absolutely 100% no reason it should stop working, then all of a sudden *after* I post my question here it suddenly starts working.... no sense. Perhaps I'll go out on a limb and say it's browser caching, because I have a 30-day mod_expires directive in my htaccess. However, I did keep my browser's cache cleared between tests.... I wonder if it's possible that Apache, itself, was sending out cached results? Or my IP caches pages? It's not my area of expertise, so I'm just guessing. Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563722 Share on other sites More sharing options...
requinix Posted January 21, 2019 Share Posted January 21, 2019 Browsers can remember redirects. Personally, I always use incognito mode when doing web development specifically so I can avoid issues like this. Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563723 Share on other sites More sharing options...
StevenOliver Posted January 28, 2019 Author Share Posted January 28, 2019 I have a followup question. I'm thinking it belongs in a new thread, so I'll try creating a new thread :-) Quote Link to comment https://forums.phpfreaks.com/topic/308169-3-redirects-https-www-and-root/#findComment-1563975 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.