I need to set up a rule to redirect all traffic/URLs with:
HTTP to HTTPS.
www to non-www. (for all subdomain levels).
Transform Rules (URL Rewrites)
With respect to the task that I need to do, what is the order of rule types I should be considering to handle it?
Regarding the “Always Redirect to HTTPS” option in the dashboard. Can I enable that and only set up a rule to deal with www, or should I set up a rule for both and disable the option? If I combine the two, would that form 2 redirects or will they be blended into 1?
I want to do this for all domains on the account so is there a way to do it like that or do I need to define the same rule for each one?
For those who know, can you please elaborate a little bit on the difference in rules, when to use what?
That would result in 2 redirects for requests that need both (http://www.example.com). You can do with one redirect by crafting a Redirect Rule and turning off AUH.
When incoming requests match:
Hostname eq "www.example.com"
Redirect Type: Dynamic
Expression: concat("https://example.com", http.request.uri.path)
Preserve query string: Yes
You need to setup rules for each zone.
I’d refer you to the documentation, as they are clearly explained. In case you try something that doesn’t work, then open a topic here for a specific question detailing what you have tried and what were the expected and actual results (error messages, redirect loop etc.)
I’ll only point out that Page Rules will at one point become obsolete, as other Rules were created to replace their functionality; please read The future of Page Rules.
Hi, @muresanmihaicristian, I’d suggest you refrain from posting ChatGPT or other AI-generated responses without first thoroughly reviewing them, as, despite your best intentions, they tend to be based in old documentation and are often incorrect, too verbose and repetitive.
If your reply was, as I suspect, AI-generated, please remove it as it contains several incorrections:
The suggested Page Rule pattern would create a redirect loop, as it also includes the target URL.
The answer to the question about redirecting several domains within an account solves a different problem, that of redirecting several hostnames within the same zone.
The description of Origin Rules is completely wrong.
The Redirect Rules description is overly and unnecessarily repetitive.
But if instead you wrote the reply yourself, I suggest you spend some time reviewing it, as well as reading the relevant documentation.
Thank you @cbrandt, I did used it to help me format while giving some instructions, I do apologise for not spending enough time onto ensuring the response was acurate and up to date with the latest documentation provided, I will keep this in mind for future, spend more time onto providing acurate feedback and ensure clarity.
I’m on cPanel and it creates www as ServerAlias for any subdomain, which makes them accessible over the www variant. That is exactly the reason I want to redirect to a version without it.
I’m aware of the fact that Universal SSL doesn’t cover deeper subdomain levels than the first. Now that you mentioned it, in case I want to cover deeper levels, what are my options with Cloudflare certs? BTW, I know that I can reissue the Origin cert to include deeper subdomains, but what about the Edge cert?
Not to forget, I’ll try out the solutions you suggested and get back to you.
Here is the big picture. I need one redirect rule to do three things:
Replace HTTP with HTTPS in URLs.
Remove www. from URLs.
Append a trailing slash / to the end of URLs. But this is a tricky one and needs explaining. I’m on WordPress and it has its own logic for URIs therefore a trailing slash is not needed always. I use a WP plugin that does this but it creates an additional redirect and I would like to form just one redirect here at Cloudflare to take care of everything. I contacted the maker of the plugin and he was kind enough to talk to me about the code that he uses in .htaccess to append a trailing slash for WP-specific logic. I will post the code here soon.
Just ignore those subdomains. As long as you don’t create DNS for them in Cloudflare, they won’t be accessible, and therefore no redirection is needed. You can also safely delete any such www.sub DNS record Cloudflare may have imported from cPanel, if applicable, since you’re not using them.
That’s a tricky one. To add the trailing slash, you’d need to exclude any possible path that you may have that does not end with a trailing slash, such as images, CSS, JS files etc, not to mention ads.txt, sitemap.xml etc.
But why would you need this? If your permalinks structure ends with a slash, your sitemap should only publicize URLs ending with a slash. Just because you can manually type in a URL without a slash, it doesn’t mean visitors will. Most visitors (I’d say >99%) will never ever type in a URL, they just click what is available in search engines, menu links, ads links etc. As long as those links are properly set, you shouldn’t worry.
I do have www subdomains in DNS and cPanel creates them by default. I import the entire zone from a file to Cloudflare and going through files to remove www records is a job I don’t want. I would rather comply with the native cPanel login and do a redirect than remedy something by hand. Plus I have a lot of domains.
Regarding the trailing slash, WordPress will serve any requested URL variant, with or without the slash. For SEO purposes I don’t want to have both wondering through SERPs. Actually, there are quite a few plugins and their makers that are solving this very neatly.
None of those redirects create an SEO issue. It would only be an SEO issue if the redirects were being advertised by you via sitemap, ads, etc, or by a third-party (but who’d do that?). If that worries you, you can disable the dictionary redirects, just online and you’ll find plenty of blogs teaching you how.
You could try to craft a Redirect Rule to add a trailing slash:
URI Path does not start with "/wp-"
AND URI Path does not end with "/"
AND URI Path does not end with ".jpg"
AND URI Path does not end with ".css"
But… it would be rife with potential collateral issues. Even if you create a perfect matching expression, it could stop working any moment a plugin is updated or installed that requires a file not matching the expression. You simply don’t want to go that direction, imho
I need to remove www and add a trailing slash and join all 4 pages into one for search engines. It is also OK to opt for URLs with www and without the slash, it doesn’t matter as long as you are consistent with using the chosen variant.
This is actually WordPress’s built-in functionality and is easy to set by adding or removing a slash after the permalink template:
This actually as far as I know doesn’t create a redirect it just sets which variant is served by WordPress.
But I’m using a great plugin called Permalink Manager Pro that allows me to customize URLs far beyond what WP is capable of but unfortunately, it disables this WP built-in capability and deals with it by adding a 301 redirect. And since I want to use Cloudflare for other redirects I would like to join all 3 things with 1 Cloudflare redirect.
These are two different use cases.
To remove WWW, you need to use what i shared above.
To redirect all HTTP traffic to HTTPS (i.e. tell clients to reconnect to HTTPS://) you need to use Always Use HTTPS Always Use HTTPS · Cloudflare SSL/TLS docs
Not possible to cover both cases at the same time as you want. The replacement suggested by @smarsh will modify the hostname by removing the first 4 characters (the www and it’s subsequent dot), so it should only apply to requests with www.. Now, suppose a requests arrives for http://example.com, where there’s no need to change the hostname, but only to change from http to https. You of course don’t want a redirect from
Also, you may be worrying unnecessarily about the http to https redirect, as most recent browser updates includes a DNS-based redirect, so that users will first request https even when they click on, or type, a http URL. Only bots will have their requests to http impacted by the double redirect. And bots include curl requests and those from most online tools.
This has already been answered.
A zone includes a domains (e.g. example.com) and its subdomains (blog.example.com, www.blog.example.com etc.)
You can write a script to copy the Redirect Rule over to all your zones using the Cloudflare API.
That’s the best decision, as WordPress knows exactly what is a front-end request, and what isn’t.
I just realized there are two more things I need this redirect to do:
I want to redirect not just the hostname, but the path and query string as well, in other words, everything after the hostname, for example, example.com/contact?page=1234.
This redirect doesn’t append the trailing slash at the end of the hostname, for example: https://www.example.com/ => https://example.com
But I want this: https://www.example.com/ => https://example.com/
I know it’s not a big deal but I want to be consistent.