Redirect all subdirectories to main domain with PageRules


#1

I want to create a page rule that will redirect all subdirectories to the main main like this:

example.com/sub/sub2 -> https://example.com
http://www.example.com/sub -> https://example.com

i.e. I want to redirect all urls to https://example.com.

As you see below, I tried to implement this, however The URL matching pattern matches “https://example.com” too, ending in an infinite redirect loop.

Is there a way to match the URL so that it matches everything but https://example.com? Thanks.


#2

Try this in .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]


#3

Unfortunetly I’m not running a web server, otherwise it would be straightforward. I have a static website.


#4

A static website still needs some sort of server software to deliver content. Typically, it’s Apache. Give the .htaccess fix a try and see if it does anything.


#5

Hello @pavsidhu,

Unfortunately there is no straighforward way to achieve that directly with the page rules, but we could with an Edge Side Code (included in the Enterprise plan).

ESC is a professional service which consists in pushing code for your domain that does specific traffic management.

As @sdayman said, even if your content is pretty static this must be rendered by a webserver some how where you’re likely able to do things like that.


#6

Hi there. Unfortunately I can’t address the specific question that this thread was created for, sorry @pavsidhu!

However I would like to maybe point out that static sites can be hosted in their entirety via AWS S3 bucket, Google Cloud Platform bucket, GitHub pages, etc. In these instances I don’t believe the user has any control over the renderer. I can check though, as I’m currently migrating one of my static sites from S3 to GCP.

@pavsidhu could you tell us if you are hosting your site this way or using another similar static-site hosting service (e.g. Netlify)?


#7

@andy Like you mentioned, I’m using an AWS S3 bucket, so therefore I have no control.


#8

Ok, thank you for confirming that.

Are you using a generator like Jekyll, Hugo, Metalsmith, etc. or are you hand-coding everything?

I only ask, because I’m curious now about the nature of your initial question. Are you trying to establish this redirect because of external sites linking to these subdirectories?


#9

It’s straight HTML, no generator has been used.

It’s an odd case, but I had a website which was running on a server. The website has now shut down, so I have a static page which states the website has been shut down. I wanted all the URLs to redirect to this page.

I just discovered a solution that uses some of the capabilities of AWS S3 buckets. I can create redirection rules in the bucket rather than through Cloudflare.

Thanks for the help anyways :slight_smile:


#10

Cool, sure no problem. Sounds like you’re on the right track! :sunglasses: