ggeo
ggeo

Reputation: 462

apache2 redirecting to https://www

I am trying to make any request that comes to my laravel website to redirect to https://www. because Google Analytics complains. After a lot of googling and reseaching my .htaccess file looks like the following. (My site is running at AWS EC2 behind an Elastic Load Balancer)

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
RewriteCond %{REQUEST_SCHEME} http [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]


</IfModule>

When I save the new .htaccess file and I restart apache I get a 504 error in Chrome saying that the website www.example.com redirected you too many times. Try deleting your cookies.

When I delete my cookies or I visit example.com from incognito it works perfectly! But how can I force the users to delete their previous cookies so that they do not get too many redirections?

Thanks

Upvotes: 1

Views: 114

Answers (2)

ggeo
ggeo

Reputation: 462

I finally found the solution. I used the X-Forwarder-Proto header to check if the request the ELB received was http or https. Then I combined the rules above and it worked!

RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
RewriteCond %{HTTP:X-Forwarder-Proto} ^http$ [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]

Upvotes: 1

Waleed Ahmed Haris
Waleed Ahmed Haris

Reputation: 1244

If you are using AWS, and you have made security rules like all https request hit ELB and ELB request instance in simple http. So this might be the case that, ELB converting https request to http. Please check your security rules first that they arent the cause ?

Upvotes: 0

Related Questions