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/48888eaa69c804bcfdc2ab6dad531d4f to your computer and use it in GitHub Desktop.
Save JoeyBurzynski/48888eaa69c804bcfdc2ab6dad531d4f to your computer and use it in GitHub Desktop.
Cloudflare Dynamic Redirect Rule: Enforce Trailing Slash in URL Paths via 301 Redirect

Enforce Trailing Slash in URL Paths via 301 Redirect

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

Review the Cloudflare Dynamic Redirect Rule below for issues:

  • Rule Name: Enforce Trailing Slash in URL Paths via 301 Redirect

Rule Expression

(
  (
    http.host eq "www.example.com" or
    http.host eq "dev.example.com" or
    http.host eq "qa.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|html?|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

  (
    not ends_with(http.request.uri.path, "/") and 
    not http.request.uri.path matches "\.[a-zA-Z0-9]{2,4}$"
  )
)

Then.. URL Redirect

  • Type: Dynamic
  • Expression: lower(concat(http.request.uri.path,"/"))
  • Status Code: 301
  • Preserve query String?: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment