Routing Rules, prevent URL directories...?

Hello all,
I hope you guys are fine,

I am so confused with how rules work on Cloudflare…
It’s not as simple as configuring .htaccess or server configuration…

I have 2 websites, I just want to prevent people from trying different URLs; there is nothing inside but the page is broken, does not look professional job…

let’s say my website is https://mywebsite.work (just an Example)

if I could set a wildcard, for example

https://mywebsite.work/* -> https://mywebsite.work
or
https://mywebsite.work/*/* -> https://mywebsite.work

I tried page rules, I did a forward 301, but I ended with a page that has no CSS or js or anything, so I did not do it right…

The objective to prevent (parent directory) and go home page…
As I said, nothing is shown but the URL would remain and the page looks broken
https://mywebsite.work\test\hello\123

it should go back to https://mywebsite.work
unless accessing a resource https://mywebsite.work\imgs\logo.png
this is fine…

I appreciate any insights on this…
Thank you very much in advance…

Yes, your CSS and JS would break, as the links to those would also be redirected to the index page and so the CSS or JS wouldn’t be served.

You seem to want to prevent 404 errors when someone visits a path that doesn’t exist. Is that correct? If so, I don’t see any reason to do that, better to create a helpful 404 page on your origin to help users find their way (and instruct bots and crawlers not to bother with that page again).

If you really want to do this, then you can trap the 404 on your origin, and then redirect is probably the easiest way. If you want to do it on Cloudflare, you’ll need to create a redirect rule that lists all the valid paths first, then redirect if none match. A lot of work if you have more than a handful of pages or things change.

2 Likes

I only have index.html to be the homepage for users to use…

Now you said 404, this is applicable with buckets, they’ll see a message handled by cloudflare …

But as a website, there is no 404, it loads the index.html but js and css are broken because the url has changed, perhaps i should update the links from ./css/somestyle.css to css/somestyle.css

But it makes no sense that index.html loads when url is just random rubbish path…

If i could throw a custom 404, great…

I’m thinking since I’m loading index.html, i just put a script to check url and update location accordingly, but it would be better to do things from server configuration than client…