Rule to redirect non https and/or subdomain uri's to https://domain.tld

What is the name of the domain?

example.com

What is the issue you’re encountering

rewrite to https and domain

What steps have you taken to resolve the issue?

I cannot get my head around the expressions

I want to redirect (301) all non-https and/or subdomain (*.example.com) to http s://example.com ,

can this be done with one rule? If so, I cannot work it out haah. Any help appreciated.

For example, while there is http.host, there doesnt seem to be a field for the zone or domain explicitly

i tried:

regex_replace(“^https?://(?:[^/]+.)?([^/]+.[^/]+)(/.*)?$”,“https://$1/$2”)

You can try to collapse this logic into one rule, but it will require you to use a custom expression:

  • Filter expression: (not ssl) or (http.request.full_uri wildcard r"https://*.example.com/*")
  • Dynamic URL redirect expression: wildcard_replace(http.request.full_uri, "http*://*.example.com/*", "https://example.com/${3}")

However, it might be easier to manage this with two rules: one to redirect HTTP to HTTPS and another one to redirect subdomain to root — both are available as one-click templates from Rules > Templates tab in the dashboard.

1 Like

thanks. I was hoping for bit more dynamic, so I wouldnt need to specify the apex domain as a string, but also do this with one rule, same as I do at the origin, as that’s the purpose here, just to redecuse the amount of non-https and www requests to the origin (and subsequent 301’s)

I have a lot of zones/domains so this is something i was hoping to push out via API.

I would have thought these 2 rules are one off the most commonly deployed so it surprises me that it’s not a bit easier to implement.