大网数据广告活动
服务器运维问答

Apache www to non-www redirect on Amazon Elastic Beanstalk

· 52CE 我爱测

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 hav…

问题原文

原帖链接 · 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.

  1. Setup an S3 bucket for www.example.com and set it up for website redirection to example.com
  2. Set the record set for www.example.com in 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。