rack-url-canonicalizer 0.2.0 → 0.3.1
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 +4 -4
- data/README.md +25 -3
- data/lib/rack/url_canonicalizer/configuration.rb +8 -0
- data/lib/rack/url_canonicalizer/version.rb +1 -1
- data/lib/rack/url_canonicalizer.rb +34 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 894b233627b42a6f6db8d6962f8622cd768b39603f1f569b79a45d413c89a95d
|
|
4
|
+
data.tar.gz: 1875213c68bdd7bb0cc6df9b84984117d98ef1d23a678c0c0268d3ee51690936
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d940708d8f98bf171dd7d4e8d521f131a1f85a58256dbcfb985551fc3aa0c61bd923da039e3a6cf5b1addc50e6c508f80f0eda4ac9045559fd58ef94912682e9
|
|
7
|
+
data.tar.gz: 1eea70a9bb3a300d90133749c1197cf06b492def21d9aa8f84b4fa823a483f37dd78ac57e76ee5699c5a9bbf23810d3712110dd0466f47689cce7c6194ddfc71
|
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.
|
|
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,10 @@ 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). |
|
|
55
|
+
| `downcase_exclude_rails_paths` | `Boolean` | `true` | Automatically preserves casing for Rails internal routes (`/rails/*` such as Active Storage signed URLs, Action Mailbox, Mailers). |
|
|
51
56
|
| `exclude_paths` | `Array<String>` | `[]` | Array of path prefixes to bypass (e.g. `%w[/api /assets]`). |
|
|
52
57
|
| `exclude_hosts` | `Array<String \| Regexp \| Proc>` | `[]` | Array of hosts or patterns to bypass from host redirection. |
|
|
53
58
|
| `locale_param` | `String` | `"locale"` | GET parameter key for locale. |
|
|
@@ -64,20 +69,35 @@ bundle install
|
|
|
64
69
|
In `config/application.rb`:
|
|
65
70
|
|
|
66
71
|
```ruby
|
|
67
|
-
# Strip www
|
|
72
|
+
# Strip www, normalize paths, and enforce lowercase:
|
|
68
73
|
config.middleware.use Rack::UrlCanonicalizer,
|
|
69
74
|
strip_www: true,
|
|
70
75
|
collapse_slashes: true,
|
|
71
76
|
strip_trailing_slash: true,
|
|
77
|
+
downcase_path: true,
|
|
72
78
|
exclude_paths: %w[/api /assets /up],
|
|
73
79
|
allowed_locales: -> { I18n.available_locales }
|
|
74
80
|
|
|
81
|
+
# Or selective downcasing for specific marketing / content routes:
|
|
82
|
+
config.middleware.use Rack::UrlCanonicalizer,
|
|
83
|
+
downcase_paths: [
|
|
84
|
+
"/about",
|
|
85
|
+
"/contribution",
|
|
86
|
+
"/pricing",
|
|
87
|
+
%r{\A/(docs|faq)/}i
|
|
88
|
+
],
|
|
89
|
+
downcase_exclude_paths: [
|
|
90
|
+
%r{\A/invites/[A-Za-z0-9_-]+}
|
|
91
|
+
]
|
|
92
|
+
|
|
75
93
|
# Or enforce www (example.com -> www.example.com):
|
|
76
94
|
config.middleware.use Rack::UrlCanonicalizer,
|
|
77
95
|
enforce_www: true,
|
|
78
96
|
exclude_hosts: %w[api.example.com]
|
|
79
97
|
```
|
|
80
98
|
|
|
99
|
+
> **Note:** Rails internal routes (`/rails/*`, such as Active Storage case-sensitive signed token URLs, Action Mailbox, and Mailer previews) are automatically protected from `downcase_path` by default (`downcase_exclude_rails_paths: true`). Setting custom `downcase_exclude_paths` will complement this default rather than overwrite it.
|
|
100
|
+
|
|
81
101
|
### Sinatra / Plain Rack
|
|
82
102
|
|
|
83
103
|
In `config.ru`:
|
|
@@ -86,6 +106,7 @@ In `config.ru`:
|
|
|
86
106
|
require "rack/url_canonicalizer"
|
|
87
107
|
|
|
88
108
|
use Rack::UrlCanonicalizer,
|
|
109
|
+
downcase_path: true,
|
|
89
110
|
enforce_www: true,
|
|
90
111
|
exclude_paths: ["/api"]
|
|
91
112
|
|
|
@@ -100,6 +121,7 @@ Alternatively, set global defaults:
|
|
|
100
121
|
Rack::UrlCanonicalizer.configure do |config|
|
|
101
122
|
config.enforce_www = true
|
|
102
123
|
config.collapse_slashes = true
|
|
124
|
+
config.downcase_path = true
|
|
103
125
|
config.allowed_locales = %w[en ru es]
|
|
104
126
|
end
|
|
105
127
|
|
|
@@ -5,6 +5,10 @@ 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,
|
|
11
|
+
:downcase_exclude_rails_paths,
|
|
8
12
|
:exclude_paths,
|
|
9
13
|
:exclude_hosts,
|
|
10
14
|
:locale_param,
|
|
@@ -18,6 +22,10 @@ module Rack
|
|
|
18
22
|
@enforce_www = false
|
|
19
23
|
@collapse_slashes = true
|
|
20
24
|
@strip_trailing_slash = true
|
|
25
|
+
@downcase_path = false
|
|
26
|
+
@downcase_paths = nil
|
|
27
|
+
@downcase_exclude_paths = []
|
|
28
|
+
@downcase_exclude_rails_paths = true
|
|
21
29
|
@exclude_paths = []
|
|
22
30
|
@exclude_hosts = []
|
|
23
31
|
@locale_param = 'locale'
|
|
@@ -8,6 +8,7 @@ require_relative 'url_canonicalizer/railtie'
|
|
|
8
8
|
module Rack
|
|
9
9
|
class UrlCanonicalizer
|
|
10
10
|
IPV4_REGEX = /\A\d{1,3}(?:\.\d{1,3}){3}\z/
|
|
11
|
+
DEFAULT_RAILS_PATH_PATTERN = %r{\A/+rails(/|\z)}i
|
|
11
12
|
REDIRECT_BODIES = {
|
|
12
13
|
301 => 'Moved Permanently',
|
|
13
14
|
302 => 'Found',
|
|
@@ -118,9 +119,42 @@ module Rack
|
|
|
118
119
|
def normalize_path(path)
|
|
119
120
|
path = path.gsub(%r{/{2,}}, '/') if @config.collapse_slashes
|
|
120
121
|
path = path.chomp('/') if @config.strip_trailing_slash && path.length > 1 && path.end_with?('/')
|
|
122
|
+
path = path.downcase if should_downcase_path?(path)
|
|
121
123
|
path
|
|
122
124
|
end
|
|
123
125
|
|
|
126
|
+
def should_downcase_path?(path)
|
|
127
|
+
return false if downcase_excluded?(path)
|
|
128
|
+
|
|
129
|
+
if @config.downcase_paths == true
|
|
130
|
+
true
|
|
131
|
+
elsif @config.downcase_paths
|
|
132
|
+
match_path_patterns?(@config.downcase_paths, path)
|
|
133
|
+
else
|
|
134
|
+
@config.downcase_path
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def downcase_excluded?(path)
|
|
139
|
+
return true if @config.downcase_exclude_rails_paths && DEFAULT_RAILS_PATH_PATTERN.match?(path)
|
|
140
|
+
return false if @config.downcase_exclude_paths.nil? || @config.downcase_exclude_paths.empty?
|
|
141
|
+
|
|
142
|
+
match_path_patterns?(@config.downcase_exclude_paths, path)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def match_path_patterns?(patterns, path)
|
|
146
|
+
Array(patterns).any? do |pattern|
|
|
147
|
+
case pattern
|
|
148
|
+
when Regexp then pattern.match?(path) || pattern.match?(path.downcase)
|
|
149
|
+
when Proc then pattern.call(path)
|
|
150
|
+
else
|
|
151
|
+
prefix = pattern.to_s.downcase
|
|
152
|
+
prefix = "/#{prefix}" unless prefix.start_with?('/')
|
|
153
|
+
path.downcase.start_with?(prefix)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
124
158
|
def locale_redirect?(query_params)
|
|
125
159
|
key = @config.locale_param
|
|
126
160
|
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.
|
|
4
|
+
version: 0.3.1
|
|
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.
|
|
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: []
|