MaxD
MaxD

Reputation: 574

Looping redirect in .htaccess

I have this simple .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} !www.mydom.net [NC]
RewriteRule (.*) www.mydom.net/$1 [R=301]

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]

Works fine, except entering with query string mydom.net/?view=xyz; it then goes into a redirect loop:

https://mydom.net/var/www/domdir/www.mydom.net/var/www/domdir/www.mydom.net/var/www/domdir/...

but more strange is why is it inserting the DocumentRoot=/var/www/domdir in the first place?

What's wrong?


Added explanation for the record: this was intended to force all requests to mydom.net to be https://www.mydom.net with or without query string.

Upvotes: 1

Views: 57

Answers (1)

Ultimater
Ultimater

Reputation: 4738

Why a query string would make a difference sounds like a non-related browser cashing issue from your 301 permanent redirect. Let's say they entered http://mydom.net/?view=xyz The first rule doesn't begin with http or https so it treats your rewrite like a directory. You'd be rewritten to http://mydom.net/www.mydom.net then, because your first rule has no L flag your 2nd rule applies, so you get rewritten to: https://mydom.net/www.mydom.net the .htaccess file is read from the top since the URL changed. no www. detected, so you get rewritten to http://mydom.net/var/www/domdir/www.mydom.net/ etc.

I believe a fix would be something close to:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.mydom.net [NC]
RewriteCond %{REQUEST_URI} ^/?(.*)$
RewriteRule ^ https://www.mydom.net/%1 [R=301,L]

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/?(.*)$
RewriteRule ^ https://%{HTTP_HOST}/%1 [R,L]

Upvotes: 3

Related Questions