AWS Elastic Beanstalk上托管的Wordpress上的HTTPS

时间:2019-02-08 18:08:50

标签: wordpress amazon-web-services .htaccess https amazon-elastic-beanstalk

我正在为我的网站强制通过HTTP协议使用https。我设法强制使用http://example.com => https://example.com和所有博客文章URL。

但是,当我尝试访问https://example.com/wp-admin时,它说example.com重定向了您太多次。 ERR_TOO_MANY_REDIRECTS

请注意:

  • 我的本地开发和AWS Elastic Beanstalk已连接到同一数据库。
  • localhostlocalhost/wp-admin可以正常工作,并且可以成功登录到管理面板。
  • 我已经设置了一个环境变量WP_ENV,以确定它是本地环境还是生产环境。

这是我的wp-config.php文件的设置。

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
    $_SERVER['HTTPS'] = 'on';

/** HTTPS  */
if (getenv('WP_ENV') == 'production'){
    define('FORCE_SSL_ADMIN', true);
    define('FORCE_SSL_LOGIN', true);
    define('WP_HOME', 'https://example.com');
    define('WP_SITEURL', 'https://example.com');
}

if(getenv('WP_ENV') == 'development'){
    /* Http only.  */
    define('WP_HOME', 'http://localhost');
    define('WP_SITEURL', 'http://localhost');
} 

这是我的.htacess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

<If "%{HTTPS} == 'on'">
# For a site running on port 80 (http)
    RewriteCond %{SERVER_PORT}  ^80$
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^wp-(admin|login|register)(.*) https://example.com/wp-$1$2 [L]
</If>

RewriteCond %{SERVER_PORT}  ^80$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

# END WordPress

1 个答案:

答案 0 :(得分:2)

在您的.htaccess文件中尝试以下操作:

# Redirect HTTP to HTTPS (all requests)
RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule ^ https://example.com%{REQUEST_URI} [R=302,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Prevent additional filesystem check
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

这假定您位于处理SSL连接的负载平衡器的后面,并且与应用程序服务器的连接实际上是通过HTTP进行的。

仅在确定可以正常工作以避免缓存问题时,才将302(临时)更改为301(永久)。

使用WordPress时,应避免编辑# BEGIN WordPress部分中的指令,因为WordPress会在更新时尝试覆盖此指令(除非您已采取其他措施来阻止此操作)。

相关问题