Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess - "Too many redirects" when trying to force https

I am trying to force a subfolder (/bbb/) of my root domain to show always as https. Also my .htaccess file take care of the extensions of the pages.

I have put the .htaccess file inside my /bbb/ folder but I get "Too many redirects" when I try to force to connect to https, without it everything works fine.

Whats wrong in my code?

Options +FollowSymLinks -MultiViews
Options +Indexes
AcceptPathInfo Off
RewriteEngine on
RewriteBase   /

ErrorDocument 404 https://example.co.uk/404page/404.html

#Force from http to https
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} !^bbb.example.co.uk/$
RewriteRule ^(.*)$ https://bbb.example.co.uk/$1 [R=301]

#take off index.html
 RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]    

## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
like image 526
SNos Avatar asked Apr 20 '16 15:04

SNos


4 Answers

If you have a proxied server or if you're using shared hosting then sometimes you'll get a free SSL via CloudFlare. And if you are using a framework like CodeIgniter or Laravel then you always have a route file. So sometimes the answer I have given above might not work.

In that case when you try to redirect to https you might get unlimited loops. So to resolve that you could try below:

RewriteEngine On

# If we receive a forwarded http request from a proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]

# ...or just a plain old http request directly from the client
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on

# Redirect to https version
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
like image 103
Lonare Avatar answered Nov 15 '22 16:11

Lonare


Problem is in this rule:

#Force from http to https
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{HTTP_HOST} !^bbb.example.co.uk/$
RewriteRule ^(.*)$ https://bbb.example.co.uk/$1 [R=301]

Change this rule to:

#Force from http to https
RewriteCond %{HTTPS} !on 
RewriteCond %{HTTP_HOST} =bbb.example.co.uk
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,NE]

You have condition reversed due to use of ! at start and have an extra slash at end which will never be matched hence making your condition always return true.

Make sure to clear your browser cache before testing this.

like image 44
anubhava Avatar answered Nov 15 '22 16:11

anubhava


Redirects in the .htaccess File

The .htaccess file is a configuration file used to modify Apache server behavior per directory on a website/server. This is a user-level configuration file, and only some Apache configurations can be edited here, though redirects are common use.

You can have multiple .htaccess files that cascade over a series of directories. If you have a .htaccess in a parent directory, and another in a sub-directory they will both affect the sub-directory. In these instances, it is important to remember where you do and do not have .htaccess files, to prevent conflicts between .htaccess files at different levels.

Below are a series of redirect examples and will aid in identifying redirects in your .htaccess file. These are not the only ways to do these kinds of redirects, but these should show you what the most common redirects look like so that you can recognize them if they are in a .htaccess file you are working with.

Force HTTPS The .htaccess code below first checks if the request came into the server using HTTP or HTTPS. If the request did not use HTTPS, then the configuration will tell the browser to redirect over to the HTTPS version of the same website and URL that was requested before.

RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Force HTTPS: When Behind a Load Balancer or Proxy (CloudFlare/Incapsula/Sucuri/etc.) Sometimes you may be using a proxy, like a load balancer or a web firewall, like CloudFlare, Incapsula, or Sucuri. These can be configured to use SSL on the front end, but not use SSL on the back end. To allow this to work correctly, you need to check not just for HTTPS in the request, but also if the proxy passed the original HTTPS request to the server using just HTTP. This following rule checks if the request was forwarded from HTTPS, and if so does not try to redirect an additional time.

RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteCond %{HTTP:X-Forwarded-Proto} =http
 RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Force non-www This redirect only checks if the website name was requested with www at the start of the domain name. If the www is included, it rewrites the request and tells the browser to redirect over to the non-www version of the domain name.

RewriteEngine On
 RewriteCond %{HTTP_HOST} ^www\. [NC]
 RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Force www This last redirect checks if the website name was not requested with www at the start of the domain name. If the www is not included, it rewrites the request and tells the browser to redirect over to the www version of the domain.

RewriteEngine On
 RewriteCond %{HTTP_HOST} !^www\. [NC]
 RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
like image 28
Vikas Jangra Avatar answered Nov 15 '22 17:11

Vikas Jangra


Inspired by this answer, I had my domain secured behind Cloudflare's WAF. Within the Cloudflare dashboard for the domain, on the SSL/TSL tab I was using the Flexible SSL/TSL encryption setting, i.e. traffic was encrypted between browser and cloudflare, but not fully end-to-end with my server. Something in this setup was causing the too many redirects.

The solution, without any additional edits to my apache config or htaccess files was to change the setting to 'Full'. This encrypts end-to-end and resolved the redirect issues immediately.

like image 29
jt_uk Avatar answered Nov 15 '22 17:11

jt_uk