0

I got the following .htaccess

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTPS_HOST} ^www\. [NC]
RewriteCond %{HTTPS_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

What I want to do is redirect everything to https://www. I thought that my htaccess should do exactly that, what's my mistake?

Evo_x
  • 2,997
  • 5
  • 24
  • 40

1 Answers1

0

Your rewrite conditions are contradictory and will stop execution of rule. You can use this single rule to do both https and add www:

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]

This will create a 302 (temporary) redirect. You can also use

RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

to create a 301 (permanent) redirect.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hmm. Am I correct in assuming that using `[R=301,L,NE]` on the last line would yield a *301 Permanent* redirect? Thanks! – jpaugh Aug 24 '15 at 03:24
  • Yes that's correct. I suggest `R=302` for easy testing of redirect, otherwise 302 is aggressively cached in browsers. – anubhava Aug 24 '15 at 03:29
  • Actually this single rule does 2 redirects: `http://servername.ru -> https://www.servername.ru` and `http://www.servername.ru -> https://www.servername.ru` but it doesn't process the last one `https://servername.ru -> https://www.servername.ru` – schel4ok Jan 06 '19 at 19:34
  • No it does only one redirect, you may have some other directive causing 2 redirects. – anubhava Jan 07 '19 at 05:54