NormalizeUrl
This gem can normalize HTTP(S) URLs by applying a certain set of transformations. After normalization, two different URLs that point to the same resource should look exactly the same.
For example:
http://example.com/productshttp://example.com/products/http://example.com/foo/../productshttp://example.com/products#comments-sectionhttp://example.com//products/http://example.com/products?
will all become http://example.com/products after normalization.
Some of the transformations are potentially dangerous, since not all webservers comform to standards and some of them are just plain weird. So there is no gurantee that the URL will still work.
Installation
Add this line to your application's Gemfile:
gem "normalize_url"
And then execute:
$ bundle
Or install it yourself as:
$ gem install normalize_url
Usage
NormalizeUrl.process("http://example.com/products/?foo=bar&baz") # => "http://example.com/products?baz&foo=bar"
Each tranformation could be skipped by demand. All you need is to pass it as a
optional value to normalize method:
NormalizeUrl.normalize("http://example.com/foo/", remove_trailing_slash: false) # => "http://example.com/foo/"
Transformations
Remove trailing slash. Option
:remove_trailing_slashExample:
http://example.com/products/->http://example.com/productsRemove repeating slashes. Option
:remove_repeating_slashesExample:
http://example.com/foo//bar->http://example.com/foo/barRemove hash fragment. Option
:remove_hash.Example:
http://example.com/foo#bar->http://example.com/fooSort query string. Option
:sort_query.Example:
http://example.com/products/?foo=bar&baz->http://example.com/products?baz&foo=bar
Contributing
- Fork it (https://github.com/rwz/normalize_url/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request