A better way to redirect https and non www traffic - SOLVED

Hi,

Attempting to minimize redirects on our sites. For example, a browser user enters:
“rosemariespeaks ‘dot’ com”

{{SORRY, HAVE TO USE ‘dot’ instead of . - system thinks I’m a spammer.}}

I want to redirect to www ‘dot’ rosemariespeaks ‘dot’ com AND force the use of SSL.

In the example below (nginx), I’m able to do this successfully. However, its a couple of redirects.

Wondering if this logic can be performed at the dns level (ie CF), and how would I go about implementing it. Thanks for your suggestions.

==============
server {
if ($host = www ‘dot’ rosemariespeaks ‘dot’ com) {
return 301 https://$host$request_uri;
} # managed by Certbot

if ($host = rosemariespeaks 'dot' com) {
    return 301 https://www 'dot' rosemariespeaks 'dot' com$request_uri;
} # managed by Certbot

listen 80;
server_name rosemariespeaks 'dot' com www 'dot' rosemariespeaks 'dot' com;

}

include /etc/nginx/301s/rosemariespeaks.map;

server {
listen 443 ssl http2;

# this line is the logic for the included map
include /etc/nginx/301s/map.conf;

server_name rosemariespeaks 'dot' com www 'dot' rosemariespeaks 'dot' com;

#... more code...

}

It’s simply the Always Use HTTPS option in the dashboard and a Page Rule to redirect from naked domain to www (without specifying http:// or https://).

I did as you prescribed, page rule 301 redirection. Cleared cache on the browsers. Removed the nginx 80 server block, reloaded nginx.
Pages can’t be found.

Have you set SSL to Full or, better, Full (Strict)?

SSL is full, LE cert has been on the site. Set https always. Just changed SSL to strict.

It isn’t resolving on Cloudflare for me though. Are the naked domain and www on :orange:?

I’ll reinsert the correct nginx and restart, one moment.

OK, nginx server block changed and nginx restarted.

The website loads for me, HTTP and HTTPS, but it isn’t passing through Cloudflare…

cf is on now for www and non www.

See if this rule is correct, in image. Then I will enable.

You need to add a * at the end of the match and a $1 at the end of the redirect, removing the final /.

did that and turned on the rule.

Seems to be working correctly, even after clearing the cache.
Tried both ‘rosemariespeaks.com’ and ‘www.rosemariespeaks.com

Current response

http://rosemariespeaks.com/aa
Location: https://www.rosemariespeaks.comaa

for the forwarding URL, you will need a slash before the $1, like /$1, so https://www.rosemariespeaks.com/$1.

Good catch, thanks for jumping in and helping out.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.