Learn about Centmin Mod LEMP Stack today
Become a Member

Cloudflare Don't forget: how to set up www redirects with Cloudflare Page Rules

Discussion in 'Domains, DNS, Email & SSL Certificates' started by deltahf, Jul 5, 2018.

  1. deltahf

    deltahf Premium Member Premium Member

    331
    149
    43
    Jun 8, 2014
    Ratings:
    +237
    Local Time:
    8:08 PM
    When I activated Cloudflare on my site, something which I forgot/didn't realize I would need to configure was the automated www redirects. I run my site with a www subdomain (www.domain.com), and redirect all non-www traffic (domain.com) to the www version with 301 redirects configured in Nginx.

    I didn't see this mentioned anywhere, so I thought I would put together a super-easy tutorial that might serve as a reminder for others in the future.

    Just create a new Cloudflare "Page Rule" with the following settings:

    Screen Shot 2018-07-04 at 10.32.17 PM.png

    Because only one Page Rule can be applied to any given request, make sure this rule is defined FIRST.

    Finally, note the "$1" in the forwarded URL. This variable ensures that anything caught by the wildcard will be in the new URL, so "mydomain.com/forum/whatever" will redirect to "www.mydomain.com/forum/whatever".

    Hope this helps someone in the future.

    [​IMG]
     
    Last edited: Jul 10, 2018
  2. eva2000

    eva2000 Administrator Staff Member

    41,090
    9,195
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,105
    Local Time:
    10:08 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    there's actually HTTPS auto rewrite option and always use HTTPS options you can enable then just remove any nginx level http to https redirects but yes you need that page rule if you want to favour one version www or non-www

    or you can do non-www https to www https redirect at nginx level