I want to redirect domain.com to www.domain.com in apache. But when I try following config It gets double redirect www.www.domain.com
<VirtualHost *:80> DocumentRoot /var/www/master/public_html ServerName www.domain.com ServerAlias domain.com RewriteEngine On RewriteRule ^(/www/.*) /www/domain.com$1 RewriteCond %{HTTPS} !=on RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP:X-Forwarded-Proto} =http RewriteRule .* https://www.%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]</VirtualHost><VirtualHost *:443> DocumentRoot /var/www/master/public_html ServerName www.domain.com ServerAlias domain.com ServerPath /var/www/master/public_html RewriteEngine On RewriteRule ^(/www/.*) /www/domain.com$1 RewriteCond %{HTTPS} !=on RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] RewriteCond %{HTTP_ACCEPT} image/webp RewriteCond %{REQUEST_URI} (?i)(.*)(\.jpe?g|\.png)$ RewriteCond %{DOCUMENT_ROOT}%1.webp -f RewriteRule (?i)(.*)(\.jpe?g|\.png)$ %1\.webp [L,T=image/webp,R] SSLEngine On ServerSignature On SSLCertificateFile /etc/ssl/certs/star_domain_com_05_2020.crt SSLCertificateKeyFile /etc/ssl/private/star_domain_com_05_2020.key SSLCertificateChainFIle /etc/ssl/certs/DigiCertCA.crt</VirtualHost>
Whats is wrong here ?
This EC2 is behind ELB which has CloudFront at front for serving static contents from S3. In Cloudfront I have set Viewer Protocol Policy :Redirect HTTP to HTTPS