I've recently moved a simple PHP site to Elastic Beanstalk but am finding it difficult to redirect the www.example.com
to example.com
. It's not https which most of the suggestions I have found are related to.
This is what I have:
Route53:
A example.com example.eu-west-1.elasticbeanstalk.com (Alias)
CNAME www.example.com example.eu-west-1.elasticbeanstalk.com
And in my .htaccess
file I have the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>
When visiting example.com
everything is fine but visiting www.example.com
gives me a redirect loop:
http://www.example.com/var/www/html/://example.com/var/www/html/://example.com
Is this the best way to redirect on AWS?
EDIT
I have now also tried
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{SERVER_NAME} ^(www\.)?(.*)$ [NC]
RewriteRule ^/?(.*)$ http://%2/$1 [L,R=301]
and while www.example.com
does redirect to example.com
, it now has the redirect loop (althought without the server path in the address bar).
%{ENV:PROTO}
for? It does not look appropriate here.