Can't map https://www.example.com to https://example.com - .htaccess

Trying to set up .htaccess to send requests for http://www.example.com to https://example.com. Using an .htaccess procedure like this
RewriteEngine On
RewriteCond %{REQUEST_SCHEME}#%{HTTP_HOST} ^http#(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
does the right thing when dealing with http://www.example.com (going to https://example.com) and either https://example.com or http://example.com. However, when I try using https://www.example.com, Chrome throws up "ERR_SSL_PROTOCOL_ERROR".
My SSL certificate apparently does not cover subdomains of example.com, so perhaps that's the problem? Is there any way to avoid unnecessary SSL processing on the www.example.com domain?
Edit: Problem was that www.example.com did not exist. After creating the sub-domain, the following htaccess code works:
# Map www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
#
# Ensure we are using https:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
[Still not sure whether the scope of my SSL cert. is an issue.]

Solution was to add the subdomain www.example.com. (See edited question.)

Related

RewriteRules redirect specific domain https to http, opposite for the others

I have a website that with a domain example.info. Recently I acquired two new domains: example.com and example.org.
I had a SSL certificate on example.info, and I was redirecting all http to https like this:
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now I want example.com to be the main url and the other two I just want to redirect to example.com.
I changed the SSL certificate from the .info to the .com and everything is good but my problem is that for example Google links me from the https://example.info and now it is failing because that url doesn't have the SSL certificate anymore. So what I want to do is to redirect https://example.info to https://example.com.
I have tried different things, but this is one of the solutions that didn't work.
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^(www\.)?example\.info$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.info$
RewriteRule ^ https://example\.com [L,R=301]
What am I doing wrong? How can I achieve to redirect https://example.info to https://example.com and keep the redirection to https for example.com?
//// EDIT
If I can't redirect https://example.info because I don't have a valid certificate anymore, how do I prevent that when I write http://example.info it gets redirected to https?
I think the best you can do is this:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.(info|org)$
RewriteRule ^ https://example\.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force HTTPS too many redirect with .htaccess, how to resolve?

I know this has been asked and answered several times and I have gone through a lot of them but still have not solved my issue.
I have the following code in my .htaccess file.
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
I want to redirect all of the following:
http://www.example.com
http://example.com
https://example.com
to redirect to https://www.example.com.
Currently it comes up to an error page stating too many redirects, I have tried clearing cookies to no avail.
What am I doing wrong and how can I make it work?
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
RewriteRule ^ https://www.%1%{REQUEST_URI} [R,L]
This canonicalises both the HTTPS and www subdomain in a single rewrite. Basically, this says... For all requests that are not HTTPS OR where the hostname does not start www then redirect to https://www.example.com/<whatever>.
The %1 backreference holds the hostname, less the optional www subdomain, from the preceding CondPattern that is always matched.
You need to use the server variable HTTPS (ie. %{HTTPS}), not the environment variable HTTPS (ie. %{ENV:HTTPS}).

redirect drupal 7 site to https

I have Drupal 7 site with default .htaccess file. I need redirect all site possibilities to one https site. What I need is:
http://example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
I have tried many options but I still get error: "This Webpage has a redirect loop."
Default .htaccess looks like:
.htaccess
[Edited] I found solution:
for remove www and retirect to https: RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]<br>
RewriteRule ^ https%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
for redirect non www to https:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]
You can also try setting the base_url setting in settings.php. By default it's commented out, but you can uncomment it and set it with the desired protocol:
$base_url = 'https://'.$_SERVER['SERVER_NAME']; // NO trailing slash!
This has saved me on multiple occasions, in particular when trying to force HTTPS from behind a proxy server, and using the Domain Access module in Drupal 7. I found the solution here.
Maybe someone is interested in these redirections (to https AND to www.*):
http://example.com to https://www.example.com
and
http://www.example.com to https://www.example.com
.htaccess:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Rewrite all subdomains to HTTPS With A Few Twists

I scoured the digital realm trying to find a solution for this because it has to be a common scenario but, alas, I failed. I didn't want to paste my MULTIPLE ITERATIONS of HTACCESS blurbs. I promise, I exhausted the lot.
Short version: Forward all subdomains to HTTPS. If no subdomain is specified, forward to HTTPS WWW.
Detailed Version:
if http or https, if there is no subdomain, forward to https www.
i.e. http://example.com OR https://example.com
would forward to
https://www.example.com
if there IS a subdomain, then forward to the HTTPS for that subdomain.
i.e. http://www.example.com
would forward to
https://www.example.com
and any subdomain, same behavior
i.e. content.example.com
would forward to
https://content.example.com
You can use that:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.htaccess Redirect Specific Subdomain from HTTPS back to HTTP

I am using .htaccess to redirect all requests (except to subdomains) from HTTP to HTTPS. This code works for me just fine:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !=m.example.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
However, it does not prevent the user from manually attempting to access the subdomain using the HTTPS (The SSL that I have does not cover subdomains and shows trust error when used for sub-domains).
Now, I am wondering:
1-How can I redirect all HTTPS requests to subdomain back to HTTP?
2-How can I modify this code to dynamically apply the subdomain restrictions to other subdomains (not only m.example.com)
1) If the certificate doesn't include m.example.com, you can never send the redirect, since the browser will refuse to make a connection. So there is no way of doing this.
2) So all you want the htaccess to do is redirect the naked domain to https. To do this use:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
I case the certificate also includes www.example.com use
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

Resources