HTTP到HTTPS重定向在Apache+Varnish for WordPress中不工作



我面临的问题是http到https重定向不工作。我目前的设置是清漆+apache。我使用apache来终止SSL。遵循本指南(https://bash-prompt.net/guides/apache-varnish/)来完成此操作(也遵循"ERR_TOO_MANY_REDIRECTS error")。部分,因为我得到太多的重定向错误)。80端口上正在运行清漆。Apache在端口8181上运行并终止SSL。尝试在htaccess中添加重定向规则,但不工作。在wp-config

中添加以下行
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS'] = 'on';
}
if ( !isset( $_SERVER['HTTPS'] ) ) {
$_SERVER['HTTPS'] = 'on';
}
define( 'WP_HOME', 'https://example.com' );
define( 'WP_SITEURL', 'https://example.com' );

当前清漆vcl配置:

vcl 4.0;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8181";
}
sub vcl_recv {
if (req.url ~ "wp-admin|wp-login") {
return (pass);
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
}

My htaccess config:

SetEnvIf X-Forwarded-Proto "https" HTTPS=on
Header append Vary: X-Forwarded-Proto
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

我几乎尝试了每一个教程。甚至尝试通过varnish vcl配置本身将HTTP重定向到HTTPS,但似乎没有工作。请帮助!

将所有用户从HTTP重定向到HTTPS

你可以使用以下任何一种解决方案。你只需要使用其中一种或全部三种,这取决于你。

1。PHP解决方案

if(!isset($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) != 'on') {
header('Location: https://www.yourdomain.com');
exit;
}

2。Apache .htaccess解决方案

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]

3。Nginx的解决方案

server {
listen 80 default_server;
server_name _;
return 301 https://$host$request_uri;
}

1。Varnish/Wordpress配置-手动设置解决方案

https://betterprogramming.pub/set-up-varnish-cache-for-wordpress-8e2ac92ce347

2。Varnish/Wordpress config - Wordpress插件解决方案

https://en-gb.wordpress.org/plugins/vcaching/

对于另一个php解决方案,您也可以在public/index.php的开头使用此方法在您的情况下,这应该更有效。

if($_SERVER['SERVER_PORT'] != 443) {
header('Location: https://www.yourdomain.com');
exit;
}
echo 'Your current server port is = '.$_SERVER['SERVER_PORT'].'<br />';

相关内容

  • 没有找到相关文章

最新更新