norman / friendly_id

FriendlyId is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids for ActiveRecord models.
http://norman.github.io/friendly_id/
MIT License
6.13k stars 589 forks source link

How to get the original URL without GUID hash #1025

Closed ronald2wing closed 2 months ago

ronald2wing commented 2 months ago

Let's say you have duplicated pages with duplicated URLs. You'd like to point the duplicated URLs to the one without the GUID hash. How would you do that?

parndt commented 2 months ago

It's a UUID so can be easily matched with a regular expression, so you could use something like this - note that there's a property friendly_id_config.sequence_separator if you use something other than -:

irb(main):003> "some-random-slug-123e4567-e89b-12d3-a456-426614174000".partition(/\-[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z/i)
=> ["some-random-slug", "-123e4567-e89b-12d3-a456-426614174000", ""]
irb(main):004> "some-random-slug-123e4567-e89b-12d3-a456-426614174000".partition(/\-[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\z/i).first
=> "some-random-slug"