Register now and start sharing your code snippets.
-->

SEO optimized image URLs with the Paperclip Rails plugin

Ruby posted about 1 month ago by christian

Create config/initializers/paperclip.rb:

   1  Paperclip::Attachment.interpolations[:permalink] = lambda do |attachment, style|
   2     attachment.instance.permalink
   3  end

In the model:

   1  has_attached_file :image, 
   2                      :path => ":rails_root/public/images:permalink/:style/:basename.:extension",
   3                      :url => "/images:permalink/:style/:basename.:extension",
   4                      :styles => { :large  => "250x360#",
   5                                   :medium => "150x230#",
   6                                   :small  => "110x150#" }

Instead of URLs like:

/images/products/249/large/temp.jpg

You can get a URL based on, for example, a permalink as in the example above. In my case I get URLs like this:

/images/games/nintendo-wii/large/super-mario-galaxy.jpg

Tagged paperclip, rails, ruby, plugin, seo, url

Split a URL into protocol, domain, port and URI using regular expressions

Java posted about 1 year ago by christian

   1  // Split URL into protocol, domain, port and URI
   2  Pattern pattern = Pattern.compile("(https?://)([^:^/]*)(:\\d*)?(.*)?");
   3  Matcher matcher = pattern.matcher(url);
   4  
   5  matcher.find();
   6  
   7  String protocol = matcher.group(1);            
   8  String domain   = matcher.group(2);
   9  String port     = matcher.group(3);
  10  String uri      = matcher.group(4);

Tagged regex, java, url