Cloudflare's global network provides a powerful foundation for speed and security, but its true potential is unlocked when you start giving it specific instructions for different parts of your website. Page Rules are the control mechanism that allows you to apply targeted settings to specific URLs, moving beyond a one-size-fits-all configuration. By creating precise rules for your redirects, caching behavior, and SSL settings, you can craft a highly optimized and seamless experience for your visitors. This guide will walk you through the most impactful Page Rules you can implement on your GitHub Pages site, turning a good static site into a professionally tuned web property.

In This Guide

Understanding Page Rules and Their Priority

Before creating any rules, it is essential to understand how they work and interact. A Page Rule is a set of actions that Cloudflare performs when a request matches a specific URL pattern. The URL pattern can be a full URL or a wildcard pattern, giving you immense flexibility. However, with great power comes the need for careful planning, as the order of your rules matters significantly.

Cloudflare evaluates Page Rules in a top-down order. The first rule that matches an incoming request is the one that gets applied, and subsequent matching rules are ignored. This makes rule priority a critical concept. You should always place your most specific rules at the top of the list and your more general, catch-all rules at the bottom. For example, a rule for a very specific page like `yourdomain.com/secret-page.html` should be placed above a broader rule for `yourdomain.com/*`. Failing to order them correctly can lead to unexpected behavior where a general rule overrides the specific one you intended to apply. Each rule can combine multiple actions, allowing you to control caching, security, and more in a single, cohesive statement.

Crafting Effective URL Patterns

The heart of a Page Rule is its URL matching pattern. The asterisk `*` acts as a wildcard, representing any sequence of characters. A pattern like `*.yourdomain.com/images/*` would match all requests to the `images` directory on any subdomain. A pattern like `yourdomain.com/posts/*` would match all URLs under the `/posts/` path on your root domain. It is crucial to be as precise as possible with your patterns to avoid accidentally applying settings to unintended parts of your site. Testing your rules in a staging environment or using the "Pause" feature can help you validate their behavior before going live.

Implementing Canonical Redirects and URL Forwarding

One of the most common and valuable uses of Page Rules is to manage redirects. Ensuring that visitors and search engines always use your preferred URL structure is vital for SEO and user consistency. Page Rules handle this at the edge, making the redirects incredibly fast.

A critical rule for any website is to establish a canonical domain. You must choose whether your primary site is the root domain (`yourdomain.com`) or the `www` subdomain (`www.yourdomain.com`) and redirect the other to it. For instance, to redirect the root domain to the `www` version, you would create a rule with the URL pattern `yourdomain.com`. Then, add the "Forwarding URL" action. Set the status code to "301 - Permanent Redirect" and the destination URL to `https://www.yourdomain.com/$1`. The `$1` is a placeholder that preserves any path and query string after the domain. This ensures that a visitor going to `yourdomain.com/about` is seamlessly sent to `www.yourdomain.com/about`.

You can also use this for more sophisticated URL management. If you change the slug of a blog post, you can create a rule to redirect the old URL to the new one. For example, a pattern of `yourdomain.com/old-post-slug` can be forwarded to `yourdomain.com/new-post-slug`. This preserves your search engine rankings and prevents users from hitting a 404 error. These edge-based redirects are faster than redirects handled by your GitHub Pages build process and reduce the load on your origin.

Applying Custom Caching Rules for Different Content

While global cache settings are useful, different types of content have different caching needs. Page Rules allow you to override your default cache settings for specific sections of your site, dramatically improving performance where it matters most.

Your site's HTML pages should be cached, but for a shorter duration than your static assets. This allows you to publish updates and have them reflected across the CDN within a predictable timeframe. Create a rule with the pattern `yourdomain.com/*` and set the "Cache Level" to "Cache Everything". Then, add a "Edge Cache TTL" action and set it to 2 or 4 hours. This tells Cloudflare to treat your HTML pages as cacheable and to store them on its edge for that specific period.

In contrast, your static assets like images, CSS, and JavaScript files can be cached for much longer. Create a separate rule for a pattern like `yourdomain.com/assets/*` or `*.yourdomain.com/images/*`. For this rule, you can set the "Browser Cache TTL" to one month and the "Edge Cache TTL" to one week. This instructs both the Cloudflare network and your visitors' browsers to hold onto these files for extended periods. The result is that returning visitors will load your site almost instantly, as their browser will not need to re-download any of the core design files. You can always use the "Purge Cache" function in Cloudflare if you update these assets.

Fine Tuning SSL and Security Settings by Path

Page Rules are not limited to caching and redirects; they also allow you to customize security and SSL settings for different parts of your site. This enables you to enforce strict security where needed while maintaining compatibility elsewhere.

The "SSL" action within a Page Rule lets you override your domain's default SSL mode. For most of your site, "Full" SSL is the recommended setting. However, if you have a subdomain that needs to connect to a third-party service with a invalid certificate, you can create a rule for that specific subdomain and set the SSL mode to "Flexible". This should be used sparingly and only when necessary, as it reduces security.

Similarly, you can adjust the "Security Level" for specific paths. Your login or admin area, if it existed on a dynamic site, would be a prime candidate for a higher security level. For a static site, you might have a sensitive directory containing legal documents. You could create a rule for `yourdomain.com/secure-docs/*` and set the Security Level to "High" or even "I'm Under Attack!", adding an extra layer of protection to that specific section. This granular control ensures that security measures are applied intelligently, balancing protection with user convenience.

Laying the Groundwork for Edge Functions

Page Rules also serve as the trigger mechanism for more advanced Cloudflare features like Workers (serverless functions) and Edge Side Includes (ESI). While configuring these features is beyond the scope of a single Page Rule, setting up the rule is the first step.

If you plan to use a Cloudflare Worker to add dynamic functionality to a specific route—such as A/B testing, geo-based personalization, or modifying headers—you will first create a Worker. Then, you create a Page Rule for the URL pattern where you want the Worker to run. Within the rule, you add the "Worker" action and select the specific Worker from the dropdown. This seamlessly routes matching requests through your custom JavaScript code before the response is sent to the visitor.

This powerful combination allows a static GitHub Pages site to behave dynamically at the edge. You can use it to show different banners to visitors from different countries, implement simple feature flags, or even aggregate data from multiple APIs. The Page Rule is the simple switch that activates this complex logic for the precise parts of your site that need it.

Managing and Testing Your Page Rules Effectively

As you build out a collection of Page Rules, managing them becomes crucial for maintaining a stable and predictable website. A disorganized set of rules can lead to conflicts and difficult-to-debug issues.

Always document your rules. The Cloudflare dashboard allows you to add a note to each Page Rule. Use this field to explain the rule's purpose, such as "Redirects old blog post to new URL" or "Aggressive caching for images". This is invaluable for your future self or other team members who may need to manage the site. Furthermore, keep your rules organized in a logical order: specific redirects at the top, followed by caching rules for specific paths, then broader caching and security rules, with your canonical redirect as one of the last rules.

Before making a new rule live, use the "Pause" feature. You can create a rule and immediately pause it. This allows you to review its placement and settings without it going active. When you are ready, you can simply unpause it. Additionally, after creating or modifying a rule, thoroughly test the affected URLs. Check that redirects go to the correct destination, that cached resources are behaving as expected, and that no unintended parts of your site are being impacted. This diligent approach to management will ensure your Page Rules enhance your site's experience without introducing new problems.

By mastering Cloudflare Page Rules, you move from being a passive user of the platform to an active architect of your site's edge behavior. You gain fine-grained control over performance, security, and user flow, all while leveraging the immense power of a global network. This level of optimization is what separates a basic website from a professional, high-performance web presence.

Page Rules give you control over routing and caching, but what if you need to add true dynamic logic to your static site? The next frontier is using Cloudflare Workers to run JavaScript at the edge, opening up a world of possibilities for personalization and advanced functionality.