问题原文
原帖链接 · Craig · CC BY-SA 3.0
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).
已采纳解答
原帖链接 · Matt Houser · CC BY-SA 3.0
This is not a solution to your .htaccess problem, but an easy redirect solution is to redirect using an S3 bucket.
- Setup an S3 bucket for
www.example.comand set it up for website redirection toexample.com - Set the record set for
www.example.comin Route 53 as an ALIAS for this bucket.
This way, when your user goes to www.example,com, they will hit the bucket and that will redirect them to example.com, which will then go to your server.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
