rack-url-canonicalizer 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 416209bee17b49fe10d7cad5c88885750a91006ab9692e37ecf115655303fa3c
4
- data.tar.gz: 354611a1a288bfbeb65c504cf5f910b483d50028b4ab8ae7b52f0d5fc68672b2
3
+ metadata.gz: 0cc0592b66830e51b4f791b9b7c9503fa98ca0c20f25fb4fe92ae9379af404ed
4
+ data.tar.gz: 572708d83ec547c190e93d78dd900db96af8de7cca0c3760122a132fef8282c6
5
5
  SHA512:
6
- metadata.gz: babc20af66280225ed938bd8f19ee0ce412552b4d39dd79d62101476c3e0526c998103f3cbdde53301057a4268f41b3bde05e6390011ad70d74ec1dd484dc902
7
- data.tar.gz: a8717237099c617244a9136e5b28c7a28b3452b6a281d0f38522c3b374c6adb9b8b96dd299870ffc639c36d24b1e35b8359b1e506b8369ce5f61418305370230
6
+ metadata.gz: 1e1b05c26326297a9b6cd425433face278ebeae8d9ca9deddf34f637d092b3f8875d64591cf5b59a0265187933cfcbe88822ef5c0d52a7c30f08b6010c19f0d8
7
+ data.tar.gz: ffc8f3651b5a9bd64a50365dd86e9a5f9129903bfd4e998d44da3a3a8a024f30a60673bfd4e6aea365954cb12299985de08c95bf12df3e0daa04ad9c59c6989e
data/README.md CHANGED
@@ -6,7 +6,8 @@ It automatically canonicalizes incoming request URLs to prevent search engine **
6
6
  1. Stripping `www.` or enforcing `www.` sub-domain prefix.
7
7
  2. Collapsing duplicate slashes (`//path//to` ➔ `/path/to`).
8
8
  3. Stripping trailing slashes from paths (`/path/` ➔ `/path`).
9
- 4. Validating `locale` GET query parameters against an allowed list and stripping invalid locales.
9
+ 4. Canonicalizing URL paths to lowercase (`/About-Us` ➔ `/about-us`), globally or selectively.
10
+ 5. Validating `locale` GET query parameters against an allowed list and stripping invalid locales.
10
11
 
11
12
  ---
12
13
 
@@ -16,7 +17,7 @@ Search engines (Google, Yandex, Bing) treat every unique URL string as a distinc
16
17
 
17
18
  This gem is essential when:
18
19
  - **Protecting SEO & Domain Authority**: You need to prevent index cannibalization caused by variations like `example.com/item` vs. `www.example.com/item`, `example.com/item/`, or `example.com//item`.
19
- - **Enforcing Single Canonical URLs**: You want incoming traffic from user typos or legacy backlinks to be redirected with an HTTP 301 (Moved Permanently) status to a single canonical address before reaching your application logic.
20
+ - **Enforcing Single Canonical URLs**: You want incoming traffic from user typos, mobile keyboard auto-capitalization, or legacy backlinks to be redirected with an HTTP 301 (Moved Permanently) status to a single canonical address before reaching your application logic.
20
21
  - **Handling Multi-Locale Query Parameters**: Your app supports localization (e.g. `?locale=en`) and you want to sanitize or discard invalid/malformed locale parameters (e.g. `?locale=xyz`) that would otherwise generate infinite duplicate URL variants.
21
22
  - **High-Performance Early Redirects**: You want URL normalization to happen at the lightweight Rack middleware layer—avoiding unnecessary Rails controller instantiations, routing checks, or database queries.
22
23
  - **Simplified Deployment with Kamal & Modern Stacks**: Ideal for containerized setups deployed with **Kamal**, **Thruster**, or Docker, where configuring and maintaining separate Nginx/Caddy redirect rules or complex DNS edge layers adds unnecessary overhead. This gem handles canonicalization directly inside your application stack for fast, zero-config deployments.
@@ -48,6 +49,9 @@ bundle install
48
49
  | `enforce_www` | `Boolean` | `false` | Prepends `www.` to requests lacking it (aliased as `prefer_www`). Automatically skips localhost and IP addresses. |
49
50
  | `collapse_slashes` | `Boolean` | `true` | Replaces multiple slashes `//` with `/`. |
50
51
  | `strip_trailing_slash` | `Boolean` | `true` | Removes trailing slash from path (except root `/`). |
52
+ | `downcase_path` | `Boolean` | `false` | Converts path to lowercase (e.g. `/About-Us` ➔ `/about-us`). Query string casing is preserved. |
53
+ | `downcase_paths` | `Array<String \| Regexp \| Proc> \| Boolean` | `nil` | Selective allowlist: only paths matching these prefixes or patterns are downcased. |
54
+ | `downcase_exclude_paths` | `Array<String \| Regexp \| Proc>` | `[]` | Array of path prefixes, patterns, or procs to bypass from downcasing (preserves casing for tokens/IDs). |
51
55
  | `exclude_paths` | `Array<String>` | `[]` | Array of path prefixes to bypass (e.g. `%w[/api /assets]`). |
52
56
  | `exclude_hosts` | `Array<String \| Regexp \| Proc>` | `[]` | Array of hosts or patterns to bypass from host redirection. |
53
57
  | `locale_param` | `String` | `"locale"` | GET parameter key for locale. |
@@ -64,14 +68,27 @@ bundle install
64
68
  In `config/application.rb`:
65
69
 
66
70
  ```ruby
67
- # Strip www (default):
71
+ # Strip www, normalize paths, and enforce lowercase:
68
72
  config.middleware.use Rack::UrlCanonicalizer,
69
73
  strip_www: true,
70
74
  collapse_slashes: true,
71
75
  strip_trailing_slash: true,
76
+ downcase_path: true,
72
77
  exclude_paths: %w[/api /assets /up],
73
78
  allowed_locales: -> { I18n.available_locales }
74
79
 
80
+ # Or selective downcasing for specific marketing / content routes:
81
+ config.middleware.use Rack::UrlCanonicalizer,
82
+ downcase_paths: [
83
+ "/about",
84
+ "/contribution",
85
+ "/pricing",
86
+ %r{\A/(docs|faq)/}i
87
+ ],
88
+ downcase_exclude_paths: [
89
+ %r{\A/invites/[A-Za-z0-9_-]+}
90
+ ]
91
+
75
92
  # Or enforce www (example.com -> www.example.com):
76
93
  config.middleware.use Rack::UrlCanonicalizer,
77
94
  enforce_www: true,
@@ -86,6 +103,7 @@ In `config.ru`:
86
103
  require "rack/url_canonicalizer"
87
104
 
88
105
  use Rack::UrlCanonicalizer,
106
+ downcase_path: true,
89
107
  enforce_www: true,
90
108
  exclude_paths: ["/api"]
91
109
 
@@ -100,6 +118,7 @@ Alternatively, set global defaults:
100
118
  Rack::UrlCanonicalizer.configure do |config|
101
119
  config.enforce_www = true
102
120
  config.collapse_slashes = true
121
+ config.downcase_path = true
103
122
  config.allowed_locales = %w[en ru es]
104
123
  end
105
124
 
@@ -5,6 +5,9 @@ module Rack
5
5
  class Configuration
6
6
  attr_accessor :collapse_slashes,
7
7
  :strip_trailing_slash,
8
+ :downcase_path,
9
+ :downcase_paths,
10
+ :downcase_exclude_paths,
8
11
  :exclude_paths,
9
12
  :exclude_hosts,
10
13
  :locale_param,
@@ -18,6 +21,9 @@ module Rack
18
21
  @enforce_www = false
19
22
  @collapse_slashes = true
20
23
  @strip_trailing_slash = true
24
+ @downcase_path = false
25
+ @downcase_paths = nil
26
+ @downcase_exclude_paths = []
21
27
  @exclude_paths = []
22
28
  @exclude_hosts = []
23
29
  @locale_param = 'locale'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rack
4
4
  class UrlCanonicalizer
5
- VERSION = '0.2.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
@@ -118,9 +118,41 @@ module Rack
118
118
  def normalize_path(path)
119
119
  path = path.gsub(%r{/{2,}}, '/') if @config.collapse_slashes
120
120
  path = path.chomp('/') if @config.strip_trailing_slash && path.length > 1 && path.end_with?('/')
121
+ path = path.downcase if should_downcase_path?(path)
121
122
  path
122
123
  end
123
124
 
125
+ def should_downcase_path?(path)
126
+ return false if downcase_excluded?(path)
127
+
128
+ if @config.downcase_paths == true
129
+ true
130
+ elsif @config.downcase_paths
131
+ match_path_patterns?(@config.downcase_paths, path)
132
+ else
133
+ @config.downcase_path
134
+ end
135
+ end
136
+
137
+ def downcase_excluded?(path)
138
+ return false if @config.downcase_exclude_paths.nil? || @config.downcase_exclude_paths.empty?
139
+
140
+ match_path_patterns?(@config.downcase_exclude_paths, path)
141
+ end
142
+
143
+ def match_path_patterns?(patterns, path)
144
+ Array(patterns).any? do |pattern|
145
+ case pattern
146
+ when Regexp then pattern.match?(path) || pattern.match?(path.downcase)
147
+ when Proc then pattern.call(path)
148
+ else
149
+ prefix = pattern.to_s.downcase
150
+ prefix = "/#{prefix}" unless prefix.start_with?('/')
151
+ path.downcase.start_with?(prefix)
152
+ end
153
+ end
154
+ end
155
+
124
156
  def locale_redirect?(query_params)
125
157
  key = @config.locale_param
126
158
  return false unless key && query_params.key?(key)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-url-canonicalizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Abramov
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0'
143
143
  requirements: []
144
- rubygems_version: 4.0.18
144
+ rubygems_version: 4.0.20
145
145
  specification_version: 4
146
146
  summary: Rack middleware for URL normalization and SEO canonicalization
147
147
  test_files: []