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]
Related
I am searching for the perfect 301 redirect HTTP to HTTPS - non-www to www. But I can't find a working solution.
Here is what I want to do
http://domain.tld/ → https://www.domain.tld/
http://www.domain.tld/ → https://www.domain.tld/
https://domain.tld/ → https://www.domain.tld/
In this post: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)
It concludes:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
but this is for www. to non-www.
Please help.
Thanks,
Not sure if you need this for multiple domains, or just one, but here is an example where multiple domains are supported. Since you want www and https on all domains, I've just hardcoded it. This should also skip rewriting if there is a sub-domain in the url. You might need to adjust it to support TLDs with dots (i.e. .co.uk, .co.au etc).
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
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.)
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]
I am working on a redirect code (that will work) for my website (it is hosted on an Apache server). What I require is a code that will redirect ALL traffic from non www to www as well as All traffic from HTTP to HTTPS.
I have tried many codes but they either do not work fully or I get stuck on a redirect loop. So basically I want a code that will redirect:
http:// .example.com AND http://www.example.com AND https://.example.com
TO
https://www.example.com
Use this .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
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}