Shahzad
Shahzad

Reputation: 21

Apache2 : too many redirects with .htaccess

Note I saw few questions already with the isssue but none helped me out.

I getting too many redirects with the following .htaccess file. I'm not expert using .htaccess but I think it also takes care of https and I really don't have to worry about it at this momoent.

RewriteCond %{HTTP} off
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^\. [NC]
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

### Remove trailing slash from all URLs

RewriteRule ^(.*)/$ $1 [R=301,L]

Upvotes: 2

Views: 60

Answers (1)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

First of all these rules are wrong :

RewriteCond %{HTTP} off
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You check whether the request comes with htpps or not , if not you do nothing except looping because in this line RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] you don't change http to https .

let's copy all rules here and change http to https in the first condition rule:

RewriteCond %{HTTP} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^\. [NC]
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The rules above make looping because the first condition RewriteCond %{HTTP} off will be done again and again whenever second condition RewriteCond %{HTTP_HOST} !^\. [NC] happened.

let's make an example :

http://www.yourwebsite.com

This request will be handled by the first condition and would be redirected to :

https://www.yourwebsite.com

Then ,it will be handled by the second condition,because it could pass it, and would be redirected to :

http://www.yourwebsite.com

Again,first condition then second and so on .this will make more looping.

Change the first rules like this :

RewriteCond %{HTTP} off 
RewriteCond %{HTTP_HOST} ^\. [NC]
RewriteRule ^(.*)   https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

As i understand you want only to force requests under this condition RewriteCond %{HTTP_HOST} ^\. into https because you except them .

Note: make sure you empty browser cache before test.

Upvotes: 2

Related Questions