Page Rule that does NOT match a URL

Is there a way to apply a rule to all pages that do not match a URL pattern?

I feel there should be a “Not” modifier for all Page Rule actions…

That’s pretty much like giving your zone a bunch of settings, then adding a Page Rule for that specific URL pattern to do something different.

Granted, there are some features that are only available in a Page Rule. In that case, an earlier Page Rule that doesn’t include this feature will trigger. It’s a bit of a kludge, but that’s what I do.

Is there a specific situation you’re thinking of?

1 Like

Yeah, very simply, I have a legacy client application that cannot handle https, and its requests have a very predictible URL pattern. I can simply add a Page Rule for it, but there is no rule to say “Disable Always Use HTTPS” (“Always Use HTTPS” is enabled for my domain).

So: I either make a Page Rule to “Always Use HTTPS”, except for a specific URL, and this is not possible; URL exceptions feature is not available.
Or: I match the URL pattern and tell it to “Do NOT Always Use HTTPS”, and this is also not possible; “Always Use HTTPS” cannot be turned off in a rule.

By the way, I also tried setting SSL off in the Page Rule, but that didn’t work. It seems that “Always Use HTTPS” is taking precedence.

How about turning off Always use HTTPS and then…
Rule 1:
Match https://example.com/specificpath/* and Redirect (301) to http://example.com/specificpath/$1

Rule 2:
Match http://example.com/* and Redirect (301) to https://example.com/$1

I’m not entirely sure if it will go into a redirect loop or not. Generally, if it matches the first rule, it doesn’t go to the second rule after execution.

1 Like

This will almost certainly cause a loop, but you have given me some ideas. I made some little tests and it seems it can be done…
The key point that I wasn’t paying attention to is that Page Rules are processed in order, and once a match is found, processing stops.
I’ll get back to testing…

Thank you :smiley:

2 Likes

Hey guys,

I need a similar thing, my static website is hosted on IPFS so can’t handle 404.

I would like to create a rule that matches any URL except 3 (that are the only one I have) and redirects to a 404.html page

I also thought about redirecting all traffic to a javascript router and do the logic there, but then users will see the URL flash to like https://mysite.com/router.html/request-path since without a server we can’t edit headers

do you have any ideas?

Well, you have a small number of valid URLs. Just make rules for them that, for example, enable SSL.
The fourth rule is the redirection to 404.
If you insist that you need to get a 404 header, redirect to a URL that doesn’t exist on your server.
If you insist that you don’t want redirection to 404, and instead just return 404 immediately, the Worker is your only hope.

Here’s a simple Worker I used once, but it outputs plain text for 2 specific URLs. Adjust it to return 404 if your URLs don’t match…

addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})

const legacyUpdaterPath0 = ‘/updater’
const legacyUpdaterPath1 = ‘/legacy/updater’
const legacyUpdaterURL = ‘http://legacy.domain.com/web/LiveUpdater/liveupdater

async function handleRequest(req) {
var response
const url = new URL(req.url)

if( url.pathname.includes(legacyUpdaterPath0) ||
url.pathname.includes(legacyUpdaterPath1) ){
response = new Response(legacyUpdaterURL, { status: 200 })
response.headers.set(“Content-Type”, “text/plain; charset=utf-8”)
} else {
response = await fetch(req)
console.log(‘Got response’, response)
}
return response
}