1

I can not find a working solution for me.

Here my settings:

# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Redirect http to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The each parts work very well but not together. The part for https redirection always redirect to https://domain.tld so without www, It is ignored. Does someone know a solution?

Thanks in advance.

satriani
  • 15
  • 4

1 Answers1

0

Forcing HTTPS and include WWW:

RewriteEngine On
RewriteCond %{HTTP_HOST}#%{HTTPS}s !^www\.([^#]+)#(?:off|on(s)) [NC]
RewriteRule ^$ http%2://www.%1%{REQUEST_URI} [R=301,L]

Should do both in one shot, otherwise you can separate the rules and probably to it this way:

# Rewrite to WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

If the rewrite doesn't work for non-www to www then there's likely a VirtualHost 301 redirect somewhere else (probably in httpd.conf) that is set on the server... It would look similar to this:

httpd.conf

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ https://example.com$1 [L,R=301]
</IfModule>

If that is set in httpd.conf it can override the rewrite in .htaccess for non-www to www.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • "Doesn't work" explains nothing... what is the behavior? – l'L'l Jun 04 '17 at 20:16
  • ``http://domain.tld https://domain.tld and https://www.domain.tld redirect to http://www./`` ``http://www.domain.tld -> 404 Page Not Found`` – satriani Jun 04 '17 at 20:26
  • rewriting loop, https part only -> ERR_TOO_MANY_REDIRECTS, www part only -> rewriting loop – satriani Jun 04 '17 at 20:42
  • It seems there is a problem with my hoster, in which it is possible only without www. Let's see what the support will say. Thank you very much for your help l'L'l – satriani Jun 04 '17 at 21:29
  • I've edited the answer once again; it may not redirect to `www` though if you have a 301 redirect somewhere else (like in virtual host settings perhaps)... – l'L'l Jun 04 '17 at 21:32