htaccess and https/non-www to https/www - .htaccess

I know there are many posts on redirects, and I've read a lot of them now, but only a few that I've found seem to touch on what I need and often seem to contain conflicting information
I need to get:
http://example.com -> https://www.example.com
http://www.example.com -> https://www.example.com
and more importantly:
https://example.com -> https://www.example.com
I can find many solutions to the first two. Like this one:
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
or this one:
RewriteEngine on
RewriteCond %{REQUEST_SCHEME} http [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
These rules look like what I should be after, but I've only got a certificate that covers https://www.example.com. Is the third redirect possible? (https://example.com to https://www.example.com). The answers in the question I linked seem to suggest it might be possible without another certificate, but there's disagreement.
I would have thought that there's no way around the problem, but I'm hoping there can be and would appreciate input.
*edit: if I don't get a certificate for https://example.com, it won't be classed as a duplicate by search engines will it?

To do a redirect between subdomains (* -> www), you need a certificate for each domain that runs https, or you need a wildcard certificate that handles them all.
The redirect is a completely separate issue.
Once you get a valid connection, you can redirect or not.
If the connection isn't valid (wrong/missing ssl cert), the redirect will never happen.

Related

301 Redirect in htaccess from NON WWW to https://www with single redirect

I have seen couple of solutions to 301 redirect non www http i.e. http://domain-name to https://www.domain-name but all of them (in my experience) gives 2 redirects.
Either they first redirect from non www to www first and than http to https or in 2nd redirect first and than 1st redirect. One of the example of such code is:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The best solution would be one 301 redirect which takes care of both i.e. non www as well as http part. Can anyone please suggest me the right code.
Best rgds,
Jai
I am using this code and I get only one 301 redirect (to https://www.example.com) when I test http://example.com with httpstatus.io
# ensure www
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The first rule, if your address doesn't start with www will be redirected to the https + www (one redirect)
The second rule applies only if your address starts with a www (because of rule 1) and it doesn't contain https. (one redirect)
Indeed, your domain http://infocera.com is getting 2 redirects.
Using #MrWhite's suggestion about your domain going through Cloudflare: you should go to your domain in Cloudflare -> Rules -> Page Rules and delete any rule you have about redirecting.
I suggest to combine both rules into a single one by combining both conditions:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
That is simple, thus easy to understand for anyone maintaining the site.
The only drawback would be that you have to specify a fixed http host in the target rule. Which is perfectly fine except if you host multiple domains and hope to find a solution working for all of them at once.
I would generally advise against such an attempt though. Such general rules have nothing to do with an application logic, one should always prefer to implement such general rule in the actual http server's host configuration instead of using distributed configuration files (".htaccess"). That means that you have separate configurations for separate domains, which again allows to place such rule into the separate configuration files while still having only a single actual redirection that is performed.

.htaccess - redirect from old domain to new and force HTTPS

I'm having trouble getting these redirects to work under all conditions. I'm hoping I can fix it with .htaccess but there may be something mucked up with the way I'd previously tried to force redirects through my host's control panel.
Anyway, olddomain.com/whatever, with http:// or https:// and with or without www should permanently redirect to https://www.newdomain.com/whatever.
At one point I had everything except https://olddomain.com redirecting properly. Now I've broken it and I'm just getting the too many redirects error.
I believe both domains have a Let's Encrypt certificate attached to them. The old domain doesn't need to be secured if that makes a difference.
According to https://serverfault.com/a/728957, you can use this snippet to help redirect users to a https://www of the site.
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Hope that helps!

HTTP to HTTPS 301 Redirection Code is Not Working, It says Too many redirects

I am using Bluehost for one of my websites. Recently, Recently, i have moved my site from HTTP to HTTPS .
After that, I have used So may different code including the below code to force HTTPS all over my website.
# SSL Rewrite
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
After using this post, when I check redirection on redirect-checker available on online, I get following messages.
But it is not working. Too many redirects. Please try to reduce your
number of redirects for http://www.example.com. You use 19 Redirects.
Ideally, you should not use more than 3 Redirects in a redirect chain.
More than three redirections will produce unnecessary load on your
server and reduce speed, which ends up in bad user experience.
And If you want to access my website from a browser, I get warning as such:
The www.example.com page isn’t working. www.example.com redirected you
too many times.
I think some redirection loop was creating such problem.
However,
My web site Status Right Now: Without using any code on htaccess
https//www.example.com is working just fine. returning 200 OK. 200 OK2
https//example.com is now 301 redirected to https//www.example.com (So, i think it is also just fine)
The problem is:
http//example.com is now 301 redirected to http//www.example.com
http//www. example.com is returning 200 OK.
To solve problem 3, I included Below code on my htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
After that I found that:
http//example.com is now 301 redirected to https//www.example.com
So the problem is solved for:
http//example.com
https//example.com
https//www.example.com
Now I just need some code to redirect 301 only:
http//www.example.com TO https//www.example.com`
Can anybody help me here?
And I am sorry if I explain my problem in a wired way... :D. The matter is I am not an expert guy on this. Hope you all will understand.
Try this:
RewriteEngine On
# If not www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# rewrite to https and www
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
# If not HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

htaccess redirect www to non-www and http to https

I found different solutions for www to non-www, http to https, or a combination of these two. However, not all of them work as expected. For example this one, it cannot fulfil my third condition below.
Conditions
1. domain.com/* -> https://domain.com/*
2. www.domain.com/* -> https://domain.com/*
3. https://www.domain.com/* -> https://domain.com/*
I combined two of the solutions I found and got the below. It works, but I am not sure if it's correctly written and used. I totally have no knowledge about using .htaccess rules. So I am hoping someone can give a hand. Thank you!
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
I think redirecting www to non-www is better done with a simple DNS CNAME record:
NAME: www.domain.com.
TYPE: CNAME
RECORD: domain.com
That would leave you with only the http to https redirect in Apache, which it looks like you have covered.

Preventing .htacess redirect chains with URL appended by trailing slash

I have a question that I wondered whether someone could help me with.
I am looking to create a redirect rule with .htaccess which sends all the following versions of the webpage to a single destination URL in one step i.e. We cannot have redirect rules kick in one after the other.
Specifically
http://example.com
https://example.com
http://www.example.com
https://www.example.com
The issue I have is that all internal pages need to be appended with a trailing slash.
Currently I can only get this to work with the redirects chaining like such:
https://example.com/example
Will then go to:
https://www.example.com/example
Before finally going to:
https://www.example.com/example/
I had a developer put something together but this seemed to cause a loop:
// Don’t correct the path when it ends in a slash
example.com^\/(.*)\/$ to https://www.example.com/$1
// Add the slash if it doesn’t end with one
example.com^\/(.*)(?<!\/)$ to https://www.example.com/$1/
Im not a developer myself but was hoping someone could help me with the correct regex? I'd be really thankful.
You can use in your .htaccess:
RewriteEngine on
# Pages without . and ev. /
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^([^.]+?)/?$ https://www.example.com/$1/ [R=301,L]
# Others
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]

Resources