Cloudflare Page Rules

Ever felt like your website wasn’t performing its best? Or maybe you’re worried about security threats lurking online. Cloudflare Page Rules can turn out to be useful.

Page Rules give you more control over how Cloudflare operates on specific URLs, domains, or subdomains. They allow you to customize Cloudflare’s settings to better meet your domain’s needs.

For instance, you could set up a rule to bypass the cache for your checkout page to ensure it always loads fresh content, or you might want to enforce HTTPS for all traffic to your site.

In this blog, we’ll discuss the benefits of page rules, how to set them up, and some of the most common page rules you can use.

Page Rules.

It enables you to customize how traffic is directed to your domain. They provide greater control over traffic management, allowing you to enhance your website’s speed, security, and overall SEO performance.

With these rules, you can:

  • Set custom caching times for different sections of your site.
  • Automatically redirect users from one part of your site to another.
  • Optimize URLs to make them more user-friendly and SEO-compliant.
  • Block specific types of unwanted or malicious traffic.

The Page Rules feature allows you to create straightforward rules by defining URL patterns and deciding the actions Cloudflare should take when those patterns are detected.

How Do Cloudflare Page Rules Work?

1. Priority

When a request is made to your site, Cloudflare checks your Page Rules in order of priority.

The first matching rule with the highest priority is the one that will be applied. It’s important to arrange your rules from the most specific to the most general to ensure the correct rules are triggered.

2. URL Pattern Matching

Page Rules operate based on URL patterns, which can include the following components:

  • Scheme (like http or https)
  • Hostname (your domain name)
  • Port (such as 8080)
  • Path (the directory structure of your site)
  • Query String (additional parameters in the URL)

For example, you could create a rule that matches all URLs under the /shop directory and sets them to be cached for 24 hours. The URL pattern might look like:

https://example.com/shop*

In this pattern, any URL that starts with https://example.com/shop will be matched. You can omit <SCHEME> and <PORT> to match both http and https and any port number.

💡 Note: To use the Rules feature, ensure that the DNS records for your domain or subdomain are proxied through Cloudflare.

Benefits of Using Cloudflare Page Rules

Cloudflare Page Rules offer several advantages that help you optimize your website’s performance, security, and user experience:

1. Enhanced Performance

By caching specific pages or assets for extended periods, Page Rules can significantly reduce load times, providing faster access to your content and reducing server strain.

2. Improved SEO

You can use Page Rules to rewrite URLs, making them more SEO-friendly. This helps improve search engine rankings and makes your site easier for users and search engines to navigate.

3. Increased Security

Page Rules add an extra layer of security, blocking malicious traffic and protecting sensitive site areas, keeping your website and users safe from potential threats.

4. Greater Control Over Traffic

Page Rules allow you to set up redirects based on user location or device type, ensuring that visitors are directed to the most relevant content and enhancing the overall user experience.

5. Flexibility and Customization

Page Rules offer a straightforward way to redirect users, cache content, or block certain traffic.

Getting Started with Cloudflare Page Rules

Before you get started, remember that Cloudflare allows you to create a wide range of Page Rules, with the number of rules depending on your plan:

Free Pro Business Enterprise
Availability Yes Yes Yes Yes
Number of rules 3 20 50 125

You can purchase additional rules for the Free, Pro, and Business plans. Each set of 5 extra rules costs $5.

Now that this is clear, we can go ahead and look at how to create page rules.

Creating Your Cloudflare First Page Rule

Before You Start

  • Execution Order: Other rules, such as Origin Rules, Cache Rules, and Redirects, can override Page Rules. Make sure you understand this hierarchy to avoid conflicts.
  • Rule Specificity: Arrange your rules from most specific to least specific for best results.

Create a Page Rule

1. Log in: Open your Cloudflare dashboard and select the domain you want to work with.

2. Go to Rules: Click on the Rules menu.

Cloudflare

3. Create a Rule: Click on the Create Page Rule button.

Create Page Rule

4. Define URL Pattern: Enter the URL or pattern you want this rule to apply.

5. Set Actions: Select the settings you want. For example, select Forwarding URL and a status code like 301 – Permanent Redirect.

6. Specify Destination: If using a redirect, enter the URL where you want users to be sent.

7. Save Your Rule: Click either Save as Draft to save it without applying it or Save and Deploy Page Rule to apply it immediately.

There are obviously more settings you can adjust depending on your need, like:

  • Always Use HTTPS
  • Automatic HTTPS Rewrites
  • Browser Cache TTL
  • Browser Integrity Check
  • Cache Deception Armor
  • Cache Level
  • Disable Apps
  • Disable Performance
  • Disable Security
  • Disable Zaraz
  • Edge Cache TTL
  • Email Obfuscation
  • Forwarding URL
  • IP Geolocation Header
  • Opportunistic Encryption
  • Origin Cache Control
  • Rocket Loader
  • SSL

Once implemented, your page rule should appear like this:

Editing or Deleting Cloudflare Page Rules

In case you wanna change or edit any page rule, follow the steps below:

To Edit a Page Rule

1. Select the Rule: Choose the Page Rule you want to edit.

2. Toggle Activation: Use the On/Off buttons to enable or disable the rule.

Deleting Cloudflare Page Rules

3. Edit Details: Click Edit to adjust the URL pattern, settings, or the rule’s order.

4. Save Changes: Make sure to save any changes you make.

To Delete a Page Rule

1. Find the Rule: Locate the Page Rule you want to delete.

2. Click Delete: Press the Delete button.

3. Confirm Deletion: In the pop-up window, confirm by choosing Delete.

confirm Deletion

💡 Note: Disabled rules won’t trigger actions but remain visible in the Page Rules tab. They still count toward your domain’s rule limit.

Recommendations for Cloudflare Page Rules

To optimize your site with Cloudflare, you can use specific page rules. Here are some common Cloudflare page rules:

1. Always Use HTTPS

This rule ensures all your visitors access your site securely via HTTPS. Setting up this page rule will automatically redirect any HTTP request to HTTPS using a 301 redirect, enhancing security.

  • Pattern to Use: https://*cloudways.com/*

  • Alternative: You can enable the HTTPS option directly in the Cloudflare dashboard under SSL/TLS settings. This saves a page rule slot. If you’re using the free version, this can be helpful since you only get 3 rules.

2. Browser Cache TTL

This rule lets you specify how long a client’s browser should cache your resources. You can set the cache duration to one year, reducing server load and improving page load times for returning visitors.

  • Benefits: Helps reduce the frequency of requests to your server and speeds up the user experience.

3. Cache Level: Cache Everything

This rule treats every resource on your web page as static, caching everything from images to HTML. This is particularly useful for reducing server load and improving site speed, but ensure your content doesn’t change frequently. Alternatively, you can also use caching plugins like Breeze by Cloudways.

  • Pattern to Use: whichserversize.net/*

4. Forwarding URL

Use this rule to redirect specific URLs on your site. For example, you could redirect old pages to new ones or use it for affiliate links. Depending on your needs, you can configure the rule to either 301 (permanent) or 302 (temporary) redirect.

5. Email Obfuscation

Enable this rule to prevent bots from scraping your email addresses and generating spam. The obfuscation feature hides your email addresses from bots while keeping them visible to human users.

However, note that it loads a JavaScript file (email-decode.min.js), which may impact performance slightly if used site-wide. Consider limiting this rule to specific pages like your contact page.

  • Pattern to Use: yourwebsite.com/contact

Boost Core Web Vitals with Cloudways’ Cloudflare Enterprise Add-on

Optimize load times, reduce latency, and meet Core Web Vitals benchmarks with precise caching, image optimization, and efficient traffic routing.

5. Don’t Cache Your Login Page

Ensure that your WordPress login page (wp-login.php) is not cached to prevent security risks and ensure real-time login functionality. This rule should set the cache level to Bypass.

  • Pattern to Use: yourwebsite.com/wp-login.php*

Remember that not all rules will be suitable for everyone, but these are some of the most commonly used ones.

Understanding Rules Priority and Ordering

When configuring Cloudflare Page Rules, understanding rule priority is crucial:

  • Rule Priority Order: Page Rules are prioritized from top to bottom in the Cloudflare dashboard. The rule listed first has the highest priority, while the last one has the lowest. Only the highest-priority matching rule will take effect on any given request.
  • How Rules are Prioritized: Cloudflare prioritizes rules in the order they are listed. Once a rule matches the URL request, it takes effect, and no further rules are checked. This means the most specific rule should be placed higher up in your list to ensure it applies.
  • Specific vs. Broad Rules: Rules like those that bypass caching for admin pages or tools should come before broader rules, such as a “Cache Everything” rule. This ensures that specific content is handled correctly before applying more general rules.
  • No Overlapping Rules: Since only one rule can apply per request, overlapping rules should be avoided to prevent unintended behavior. Combining multiple actions into a single rule can help streamline rule application.

Testing Your Cloudflare Page Rules

To test if your Page Rules are working, you can manually visit the URLs to see if the expected behavior occurs.

For example, if you have set up a forwarding Page Rule to redirect subdomain.yoursite.com to yoursitename.com, visit the subdomain to check if the redirection happens as intended.

Remember that no external indicators, such as headers, specify which Page Rule is being applied. Therefore, testing often involves observing the outcome directly in the browser.

Troubleshooting Common Page Rule Issues

It’s common to encounter issues when setting up a page rule on your website. Here are some common page rule errors and how to fix them:

Example 1: Page Rule Not Working Due to DNS Configuration

Issue: The Page Rule (e.g., URL forwarding) isn’t applied.

Root Cause: This happens if the DNS record (e.g., subdomain.yoursite.com) isn’t proxied by Cloudflare. Cloudflare’s rules won’t apply without proxying, and requests go directly to your server.

Resolution: Ensure the DNS record is set to be proxied (orange cloud icon) in Cloudflare’s DNS settings.

Example 2: Error 500 (Internal Server Error) Due to Wildcard Misconfiguration

Issue: Error 500 appears when a URL matches a Page Rule.

Root Cause: This can occur if the Page Rule uses placeholders ($2) that don’t match the number of wildcards. For instance, a rule with one wildcard shouldn’t use $2.

Resolution: Adjust the rule to use the correct placeholder. For a single wildcard, use $1 instead of $2.

You may also encounter other errors; however, troubleshooting solutions can be found easily in Cloudflare documentation.

Summary

Page Rules allow you to customize Cloudflare’s features to enhance your site’s performance, reliability, security, and overall user experience. You can modify, remove, and prioritize these rules from the most specific to the least specific settings.

In this blog, we covered how to create, edit, and delete Cloudflare Page Rules. We also reviewed some common Page Rules you might want to use on your website and provided troubleshooting tips for when things don’t work as expected.

We hope this detailed guide has helped you better understand and set up Cloudflare Page Rules. If you have any questions or feedback, feel free to share them in the comments section below.

Abdul Rehman

Abdul is a tech-savvy, coffee-fueled, and creatively driven marketer who loves keeping up with the latest software updates and tech gadgets. He’s also a skilled technical writer who can explain complex concepts simply for a broad audience. Abdul enjoys sharing his knowledge of the Cloud industry through user manuals, documentation, and blog posts.

Similar Posts