Reference: https://developers.cloudflare.com/rules/url-forwarding/
- Rule Name:
Enforce Lowercase URL Paths via 301 Redirect
- Custom filter expression: The rule will only apply to traffic matching the custom 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]"
)
- Type:
Dynamic
- Expression:
lower(http.request.uri.path)
- Status Code:
301
- Preserve query string?:
true
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.
The rule is activated when incoming requests meet the following criteria:
The request is for one of the following domains:
- www.example.com
- qa.example.com
- dev.example.com
- 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.
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.
- 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.
- 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.