I'm trying to force non-www + https in .htaccess on an AWS EC2 instance.
While there is an abundance of apparently working solutions right here on StackOverflow, all of those only produce redirect loops for me.
I get a redirect loop when I try this rule:
RewriteEngine on
#RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
#RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
(via Force non-www and https via htaccess)
Same for this one:
RewriteEngine on
#RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
#RewriteRule ^(.*)$ https://domain.com/$1 [L,R=301]
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
Both seem to work for the respective OP, and as indicated by some of the comments, they work for others too.
I have the following VirtualHosts set up in my httpd.conf
:
NameVirtualHost *:80
Listen 8443
NameVirtualHost *:8443
<VirtualHost *:80 *:8443>
ServerAdmin webmaster@domain.com
DocumentRoot /var/www/domain.com
ServerName domain.com
ServerAlias *.domain.com
ErrorLog logs/domain.com-error_log
CustomLog logs/domain.com-access_log common
</VirtualHost>
For context: The :8443 port receives traffic from an AWS ELB (load balancer) which routes :443 SSL requests to this particular port, because the SSL certificate is installed on the load balancer itself.
What could be the issue for the redirect loops?