Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeyBurzynski/9863fe94a84e226d526ae8cc8d3a8ad0 to your computer and use it in GitHub Desktop.
Save JoeyBurzynski/9863fe94a84e226d526ae8cc8d3a8ad0 to your computer and use it in GitHub Desktop.
Cloudflare Dynamic Redirect Rule: Enforce Lowercase URL Paths via 301 Redirect

Enforce Lowercase URL Paths via 301 Redirect

Reference: https://developers.cloudflare.com/rules/url-forwarding/

Cloudflare Dynamic Redirect Rule

  • Rule Name: Enforce Lowercase URL Paths via 301 Redirect

If... When incoming requests match…

  • Custom filter expression: The rule will only apply to traffic matching the custom expression

When incoming requests match… Rule Expression

(

  (http.host eq "www.example.com" or
    http.host eq "qa.example.com" or
    http.host eq "dev.example.com") and

  not http.request.uri.path matches "\.(7z|apk|avi|avif|bin|bmp|bz2|class|css|csv|dmg|doc|docx|ejs|eot|eps|exe|flac|gif|gz|ico|iso|jar|jpeg|jpg|js|json|mid|midi|mkv|mp3|mp4|ogg|otf|pdf|pict|pls|png|ppt|pptx|ps|rar|svg|svgz|swf|tar|tif|tiff|ttf|webm|webp|woff|woff2|xls|xlsx|zip|zst)$" and 

  not http.request.uri.path contains "/api/" and

  not http.request.uri.path contains "/_next" and

  http.request.uri.path matches "[A-Z]"
)

Then... URL redirect

  • Type: Dynamic
  • Expression: lower(http.request.uri.path)
  • Status Code: 301
  • Preserve query string?: true

Overview

The rule's primary function is to redirect incoming HTTP requests with uppercase characters in the URL path to a lowercase equivalent using a 301 redirect status code, ensuring consistency and improving SEO. This rule applies to specific domains and excludes certain paths and file types from redirection.

Applicability Criteria

The rule is activated when incoming requests meet the following criteria:

Domain Matching

The request is for one of the following domains:

Path Exclusions

  • The request path does not match a regex pattern specifying various file extensions, such as images, documents, archives, and more, to prevent redirection of static resources where case might be relevant.
  • The request path does not contain /api/, to exclude API endpoints which may be case-sensitive.
  • The request path does not contain /_next, to exclude Next.js generated paths which may require case sensitivity.
  • Case Sensitivity Check: The request path contains at least one uppercase character ([A-Z]), indicating a need for redirection to a lowercase version.

Redirection Configuration

Upon meeting the above criteria, the rule performs the following actions:

  • Type: Dynamic
  • Expression: Redirects the request to the lowercase version of the URL path (lower(http.request.uri.path)).
  • Status Code: 301 (Moved Permanently), indicating a permanent redirection.
  • Preserve Query String: true. The redirection retains any query parameters present in the original URL, ensuring that the request's intent is preserved.

Rationale

  • SEO and Consistency: Lowercase URLs are preferred for consistency and SEO. Search engines may treat URLs with differing cases as separate pages, leading to potential issues with page ranking and duplication.
  • User Experience: Enforcing a consistent URL structure improves user experience and prevents confusion.
  • Compliance and Standards: Adhering to lowercase paths is a common practice in web development, aligning with general URL standards.

Exclusions Rationale

  • Static Resources: Excluding specific file extensions prevents issues with accessing resources that may have case-sensitive paths.
  • API Endpoints: Excluding API paths ensures that API functionality is not disrupted due to case sensitivity requirements.
  • Next.js Assets: Excluding paths containing /_next avoids breaking Next.js applications, which may rely on case-sensitive routing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment