Home > Enterprise >  HTTP to HTTPS redirect not working in Apache Varnish for WordPress
HTTP to HTTPS redirect not working in Apache Varnish for WordPress

Time:10-26

The issue I am facing is http to https redirect is not working. My current setup is varnish apache. I am using apache to terminate SSL. Followed this guide (https://bash-prompt.net/guides/apache-varnish/) to do this (also followed "ERR_TOO_MANY_REDIRECTS Error" part in that guide as I was getting the too many redirects error). Varnish is running on port 80. Apache is running on port 8181 and terminating SSL. Tried adding redirect rules in htaccess but it is not working. Added the following lines to 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' );

My current varnish vcl config:

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

I have literally tried almost every tutorial. Even tried redirecting HTTP to HTTPS via varnish vcl config itself but nothin seems to be working. Please help!

CodePudding user response:

To Redirect all users from HTTP to HTTPS

You can use any of the solutions below. You only need to use one or all three its up to you.

1. PHP Solution

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

2. Apache .htaccess solution

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

3. Nginx Solution

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

1. Varnish / Wordpress config - Manual setup solution

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

2. Varnish / Wordpress config - Wordpress plugin solution

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

CodePudding user response:

For another php solution you can also use this at the start of your public/index.php This should be alot more effective in your case

if($_SERVER['SERVER_PORT'] != 443) {
    header('Location: https://www.yourdomain.com');
    exit;
}

echo 'Your current server port is = '.$_SERVER['SERVER_PORT'].'<br />';
  • Related