How to properly configure DNS and Flexible SSL for WordPress hosted on OpenShift through CloudFlare

TL;DR: I want to redirect https to http on all pages except for admin/login, where I want the exact opposite to happen. I also want www redirected to bare domain name. (UPDATE: Check Update 3 for the answer)

As is probably clear from the title, I have a WordPress blog hosted on OpenShift for free. I have a custom domain bought from GoDaddy. I’m using cloudflare so I can have free SSL.

Read More

Here’s my configuration:

CloudFlare DNS:

CloudFlare DNS

CloudFlare Page Rules:

CloudFlare Page Rules

This is what worked best. I actually wanted to have this rule:

*ghostlessmachine.com/* -> https://ghostlessmachine.com/$1

But I ran into even more problems like that, even though it seems to be pretty much what I’m supposed to do according to this CloudFlare article. Actually, initially I wanted to only force SSL in admin pages, but I didn’t even know how to attempt that. I thought of using two page rules, like this:

*ghostlessmachine.com/* -> http://ghostlessmachine.com/$2

*ghostlessmachine.com/wp-* -> http://ghostlessmachine.com/$2

But I had no luck.

Here’s my OpenShift configuration:

OpenShift aliases

When I write ghostlessmachine.com in my address bar, it correctly takes me to https:.... I have shared a link, however (https://ghost...), and one person has reported not being able to access it. I couldn’t reproduce locally.

When I try www.ghost..., I get:

This webpage has a redirect loop

ERR_TOO_MANY_REDIRECTS

Does anybody have any idea what I’m doing wrong? I’ve lost track of how many different configurations I’ve tried, but nothing seems to work.

Thanks!

UPDATE

OK, so following the advice in the comment I managed to get the situation a bit better. Still it’s counter intuitive for me how the article I initially linked to just didn’t get the job done while the other SO question did. So here’s what I’ve changed:

  1. Deleted the www.ghost... alias from OpenShift.
  2. Changed CloudFlare’s CNAME record from www -> blabla.rhcloud.com to www -> ghostlessmachine.com
  3. Created this Page Rule: www.ghostlessmachine.com/* -> http://ghostlessmachine.com/$1

Now both ghost... and www.ghost... work and take me to http://ghost.... However, if I type https://ghost..., it also works without redirecting me to simple http. This is a problem.

I tried using this Page Rule instead:

ghostlessmachine.com/ -> http://ghostlessmachine.com/$2

So that I got https://, http://www, www, everything redirected to http://ghost..., but it doesn’t work. I can’t access my blog anymore and whatever address I try I get ERR_TOO_MANY_REDIRECTS.

UPDATE 2

Here’s my full setup after all suggestions:

htaccess:

enter image description here

wp-config.php:

enter image description here

CloufFlare:

enter image description here

Result:

  • https -> http on non-admin/login pages: WORKING ✓
  • Trying to access admin/login pages: ERR_TOO_MANY_REDIRECTS

enter image description here

Update 3

This did the trick:

enter image description here

I still don’t understand why this works and the rest doesn’t though. This was basically a series of rather blind trial and error with some input from Allen here and Simon in the CloudFlare support page. In any case, all my requirements are respected now, thanks!

Related posts

1 comment

  1. make sure following in your wp-config.php file:

    define('FORCE_SSL_ADMIN', true);
    define('FORCE_SSL_LOGIN', true);
    

    look over here: Force non-WWW but force https across all of wordpress & the entire domain

    for redirect everything else to non-https, you can add following into your root .htaccess file, before the wordpress rewrite:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} =on
    RewriteCond %{REQUEST_URI} !^/wp-admin.*
    RewriteCond %{REQUEST_URI} !^/wp-login.*
    RewriteCond %{HTTP_REFERER} !^https://.*
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L]
    
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    Update:
    CloudFlare’s Page rule has following “Page rule priority is determined by their position in the list. If multiple rules match a URL, rules at the top take higher priority. “

    let’s see what happens before:

    1. request to https://www.ghostlessmachine.com/wp-admin hit the first
      rule, match found, then it goes to
      http://www.ghostlessmachine.com/wp-admin!
    2. now here comes http://www.ghostlessmachine.com/wp-admin, first rule,
      no rewrite, goes down to 3rd rule, oops, it needs goto
      https://www.ghostlessmachine.com/wp-admin!

    this is how the loop comes

Comments are closed.