redirect drupal 7 site to https - .htaccess

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]

Related

Most efficient htaccess url redirects?

I'm looking for the most efficient way to redirect incoming urls to the https: version of our site. What we have in place works (see below), I'm just wondering if there's a better way to do it, and to minimize the number of redirects.
Redirecting should cover the following cases:
(Domain level)
http://www.example.com - > https://www.example.com
http://www.example.com/dir -> https://www.example.com/dir
http://example.com -> https://www.example.com
http://example.com/dir -> https://www.example.com/dir
www.example.com -> https://www.example.com
www.example.com/dir -> https://www.example.com/dir
example.com -> https://www.example.com
example.com/dir -> https://www.example.com/dir
(Subdomains)
http://subdomain.example.com -> https://www.example.com/subdomain
subdomain.example.com -> https://www.example.com/subdomain
Here's what we have in our htaccess file:
# Rewrite subdomains to folders, add www if not present
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# Rewrite anything not previously caught
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks in advance!
By your rules you forced both main-domain and sub-domain into https but only non-www of main-domain into wwww so , to get it work for both do this :
RewriteCond %{HTTP_HOST} !^www [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Note: clear browser cache the test

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

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.)

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}).

Redirecting http to https via .htaccess

Currently I have the following in my .htaccess file for a forum site.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/?$ http://example.com/forums/ [L,R=301]
It works perfect for redirecting everything like (http)://example.com, www.example.com, (http)://www.example.com etc. to example.com/forums which is what I wanted.
I now want to have everything redirect to (https)://example.com/forums but am running into problems with existing links such as example.com/forums/stuff.
I can only get example.com to redirect to https://example.com/forums, any direct links such as example.com/forums/stuff etc. will not redirect to the https version, any ideas?
You need 2 redirect rules. One rule to redirect landing page to forums page and another rule to redirect http -> https:
RewriteEngine On
# redirect landing page to /forums/
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com [NC]
RewriteRule ^/?$ https://%{HTTP_HOST}/forums/ [L,R=301]
# redirect http -> https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

.htaccess http to https redirect all pages to homepage

I just purchase my site SSL license, I googled and try a few different codes to be added to htaccess.
1st set of code I used
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
2nd set of code I used
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
ref:https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
However, the site keep redirects all inner pages without https to https homepage.
Eg. http://example.com/tag1/tag2 to https://example.com
But I need it to redirect from http://example.com/tag1/tag2 to https://example.com/tag1/tag2
Is there something wrong with my code? or it server configuration related?

Resources