Creating a shortened URL for all objects in the database - ruby-on-rails

I would like to display a shortened URL besides the content items on my site for ease of sharing.
What would be the most efficient way of doing so, and are there any suitable gems / libraries?
I am using rails on a mongodb/mongoid stack

should be simple enough (regardless if you are on Mongo / MySQL or anything else). what you need is a small collection (mongo if i may) that holds some kind of an MD5 hash of the real url you are after and the real url itself, for example:
ShortLink.create(:hash_link => Digest::MD5.hexdigest(resource_url(#resource)), :real_link => resource_url(#resource))
I suggest adding another route that catches those like this:
match "l/:key", "ShortLinks#show"
should be easy.

I think you can use bitly gem to shorten your URL.
The following link helps you to configure bitly:
http://www.marketingformavens.com/blog/url-shortening-bitly-ruby-on-rails

Related

How can I show the name of the link without http://, https://, and everything that goes after .com and other similar domains?

In my view I'm displaying the link in a such way:
<%= #casino.play_now_link %>
So, #casino.play_now_link can be like this: https://www.spinstation.com/?page=blockedcountry&content=1 What I need, is to display only this part: www.spinstation.com. I tried gsub('http://', '').gsub('https://', ''), and it works, but how can I remove the part of url name after .com? Thanks in advance.
Don't use regexes at all for this sort of thing, use URI from the standard library:
URI.parse(#casino.play_now_link).hostname
or, for a more robust solution, use Addressable:
Addressable::URI.parse(#casino.play_now_link).hostname
Of course, this assumes that you've properly validated that your play_now_links are valid URIs. If you haven't then you can add validations that use URI or Addressable to do so and either clean up existing play_now_links that aren't valid URIs or wrap the parsing and hostname extraction in a method (which is a good idea anyway) with some error handling.
In a simple way one can use
.split('/')[2]
which is regex based and depends on the '/' in your url.
But as #mu is too short mentioned: URI is better for this.

Get ASP.NET bundle URL for use with CDN?

I'm using the ASP.NET bundling feature and want to know how I can get the URL returned by the Render helpers such as Scripts.Render("~/bundles/scripts").
Currently the optimized output is has a relative URL. I want to use a CDN that does origin-caching, so the final URL needs to be something like http://static.mydomain.com/bundles/scripts?v=XXXXXX
My plan is to simply write my own helper method, but I can't figure out how to get the version number relative URL for a given bundle.
The solution was rather simple. There is a static method Scripts.Url and Styles.Url that give me exactly what I want. I was able to incorporate this into my own helper to concatenate the CDN's base URL.

Asp.net url rewriting, without CNAME

I have to use URL filtering in one of my sites.
Take for example the site:
http://default.net
and I wish I could add something to the URL and filter based on that.
http://foo.default.net
In writing this I would like to retrieve the "foo" and can use it.
This URL filtering should be done without CNAME (ie dynamic).
PS: Note that I do not want http://default.net/foo but http://foo.default.net.
You may take a look at the following article which illustrates how you could define domain routes.

How to check if URLs match, within a huge database of online products?

So, the problem seems simple at the beginning but is not. Using Mongo and Node.js.
Problem: I have a URL. I need to match that URL with all the URLs I have in my database. Remember, there is no rule that the URL I'm on always have "category" infront or things like that. And please don't take "cases" into consideration.
I have no clue of the name of parameters, or anything else.
Let's assume the URL is smth like example.com/category/product_name.html?session_id=2423412fd
In the database I only have example.com/product_name.html
The URL is smth like example.com/index.php?productid=6&category=3&utm_campaign=google&utm_source=click
In the database I only have example.com/index.php?productid=6
The URL is smth like example.com/product_name.html
In the database I only have example.com/category/subcategory/product.html
I think I made my point. What I'm looking is a solution that matches URL in any cases (they are more than these). It can be an external services, class or something complex.
But I need it to work, and to work very fast because is doing this on every page refresh.
Thank you!
I would use this function to separate the strings http://php.net/manual/en/function.parse-url.php
Then take parts of the path name which you want to match from the URL and query your database URL's looking for matches.
To follow on from Anagio's answer, the URL
example.com/index.php?productid=6&category=3&utm_campaign=google&utm_source=click
could be saved as a Mongo object like:
{
url: "example.com/index.php?productid=6&category=3&utm_campaign=google&utm_source=click",
indexes: [
"example.com",
"index.php",
"productid=6",
"category=3",
"utm_campaign=google",
"utm_source=click"
]
}
You could then split up any new URL using the same algorithm, then do a map/reduce on the indexes field for scoring and then take the highest score as the best "fuzzy match"

dynamic seo title for news articles

I have a news section where the pages resolve to urls like
newsArticle.php?id=210
What I would like to do is use the title from the database to create seo friendly titles like
newsArticle/joe-goes-to-town
Any ideas how I can achieve this?
Thanks,
R.
I suggest you actually include the ID in the URL, before the title part, and ignore the title itself when routing. So your URL might become
/news/210/joe-goes-to-town
That's exactly what Stack Overflow does, and it works well. It means that the title can change without links breaking.
Obviously the exact details will depend on what platform you're using - you haven't specified - but the basic steps will be:
When generating a link, take the article title and convert it into something URL-friendly; you probably want to remove all punctuation, and you should consider accented characters etc. Bear in mind that the title won't need to be unique, because you've got the ID as well
When handling a request to anything starting with /news, take the next part of the path, parse it as an integer and load the appropriate article.
Assuming you are using PHP and can alter your source code (this is quite mandatory to get the article's title), I'd do the following:
First, you'll need to have a function (or maybe a method in an object-oriented architecture) to generate the URLs for you in your code. You'd supply the function with the article object or the article ID and it returns the friendly URL with the ID and the friendly title.
Basically function url(Article $article) => URL.
You will also need some URL rewriting rules to remove the PHP script from the URL. For Apache, refer to the mod_rewrite documentation for details (RewriteEngine, RewriteRule, RewriteCond).

Resources