Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing http using .htaccess

Tags:

php

.htaccess

This is the script I have right now, how do I have my script force all traffic to http, currently it is doing the exact opposite, it is forcing all traffic to https.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I've also tried this and it didn't work

RewriteEngine On
RewriteCond %{HTTP} !=on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I got this error:

Too many redirects occurred trying to open www.blankpage.com .

like image 431
Arian Faurtosh Avatar asked Oct 07 '13 17:10

Arian Faurtosh


People also ask

How do I force a HTTPS connection?

How Does HTTPS Redirection Work? In the Domains interface in cPanel (Home >> Domains), there's an option to enable Force HTTPS Redirection from the insecure version (HTTP) to the secure version (HTTPS) with a toggle switch.


1 Answers

You want to check that HTTPS is on:

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

And if it is on (%{HTTPS} on), redirect to http://. There is no mod_rewrite variable called %{HTTP}, only %{HTTPS} which can be "on" or "off".

The reason why you were getting the too many redirects error is because:

RewriteCond %{HTTP} !=on

is always true no matter if the request is http or https, since the variable doesn't exist, it will never be equal to "on". Therefore, even if the request is http, you keep getting redirected to the same URL (http).

like image 79
Jon Lin Avatar answered Sep 23 '22 08:09

Jon Lin