Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect – differences between two redirects

Tags:

.htaccess

Could you explain the difference between the two .htaccess redirects below?

The first redirect is the one I tend to use the most but it didn't work on a recent site (too many redirects – even though I didn't have any set up) but the second redirect worked and I'm curious.

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

RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
like image 266
John the Painter Avatar asked Nov 07 '22 17:11

John the Painter


1 Answers

The first RewriteCond checks the HTTPS flag set by the server (Check this link. Scroll to Server Variables).

The second RewriteCond checks an environment variable, which could be set by a prior RewriteRule (See Setenvvars for setting an environment variable).

Did you try using %{HTTPS} !=on?

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

Note: Both links refer to the apache documentation. Depending on your used HTTP Server this might not work.

like image 121
cronide Avatar answered Nov 23 '22 23:11

cronide