Instagram style unique URLs - url

I am developing a CakePHP application which allows for photo uploads. It would be really nice if i could give the user a friendly unique URL of something like instagram does, ie:
http://example.com/v/157fd to view their single photo post.
What is the best approach for this, i take it its going to involve CakePHP routing at some point, but has someone got an flow of what would happen when a URL like this is hit and how these unique URLs are formed?
Thanks

the route would be easy with one line (change the controller and action to yours):
Router::connect('/v/*', array('controller' => 'photos', 'action' => 'view'));
You can just use the photo id, or create your own random id. There's also a convenient way in cake to have random id, which is to use UUID

Related

Passing hidden params into url

I have a rails application in which I would like to generate a url based on a parameter, but for that parameter to be hidden from public view. So essentially working like a POST request but being able to be typed in like a GET request.
For example using a QR reader I could have the address as www.site.com/qr?lot_no=18007 but when a user scans the QR image it only shows www.site.com/qr but displays the results related to lot_no=18007.
Not sure if this is possible or not. But any help would be greatly appreciated.
If all you want to do is to prevent it from showing up in the address bar of the browser, you could use Rails.ajax to make the request dynamically through Javascript.
That will at least hide it from casual inspection, but there is no way to suppress the parameters from the query string on a GET completely, so anyone looking at the Networks tab in the browser (for example) would still see them.
Another alternative would be to encrypt the parameter value.
Maybe the Friendly id gem may be of help here
The following is an example of it's use is
http://example.com/states/washington
instead of:
http://example.com/states/4323454
This is not going to work with a post request as you mention though. It is a way of using a get request that would normally send an id in the params hash to retrieve existing records.
The gem can be found here https://github.com/norman/friendly_id
It is well maintained and up to date
Configure different route for public facing URL, the URL should include encrypted lot id as a path param.
routes.rb
get '/view_lot/:id' => 'QrCodesController#view_lot`, as: :public_view_lot
now in QrCodesController add action view_lot
def view_lot
encrypted_id = params[:id]
id = decrypt_it(encrypted_id)
#lot = Lot.find(id)
render "your_template"
end
in you QR code generation, pass URL to above action with encrypted id like public_view_lot_url(lot_id)

UUIDs & Search Engine Friendly URLs

Our SaaS app, currently in beta, allows users to generate conversations. A conversation's URL currently looks like this:
http://example.com/conversations/view/4c6a4ab4-4795-4a13-a3d9-d9d22cac28e5
I'd like to change the URL to something search engine friendly like this:
http://example.com/conversations/this-is-a-great-conversation-that-you-need-to-join
However, since conversation can potentially have the same title, we'll need the conversation's ID in there. But, that would give us a really nasty looking URL:
http://example.com/conversations/this-is-a-great-conversation-that-you-need-to-join/4c6a4ab4-4795-4a13-a3d9-d9d22cac28e5
Does anyone have any ideas on how to include a smaller unique ID in the URL so that we don't have to include the UUID?
believe me, it's not seo friendly at all:
http://example.com/conversations/this-is-a-great-conversation-that-you-need-to-join
it's more spammy than seo friendly url. since google reduced value of keywords in url
http://searchengineland.com/googles-matt-cutts-on-keywords-in-the-url-16976
keywords in url should not be more that 4 or 5 keywords.
i think you should reduce you url size and depth levels. for instance:
http://example.com/c584501
can work for you. "c" in the beginning means "conversations" you can identify your url by a character without using more levels like:
http://example.com/conversations/584501
the shorter url the more chance of sharing it on social medias.
you can use id of your table in database or use uniqueId if you use php:
http://php.net/manual/en/function.uniqid.php
i usually use my table id and i convert it to base36 with base_convert()
http://php.net/manual/en/function.base-convert.php

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.

Cakephp url customisation for SEO

I am developing a medical products search site. I need to display my site in search engines whenever a user try to search [COMPANY NAME] medicine [DISEASE]. For this, i created a page in my site which reads COMPANY NAME and DISEASE from url and lists all products.
Now i need to give this page a url like www.sitename.com/[COMPANY NAME]_medicine_[DISEASE].html
I am using Cakephp framework for development. Is there anyway to implement this url formatting in routes.php ? Or is there any other way ? please help.
how about separating them with slashes?
// www.sitename.com/[COMPANY NAME]/medicine/[DISEASE]
Router::connect('/:company/medicine/:disease', array('controller' => 'diseases', 'action' => 'index'),
array('pass'=>array('company','disease'),
'company'=>"[a-zA-Z\.]+*",
'disease'=>'[a-zA-Z\.]+'));
and the controller
function diseases($company,$disease){
}
I'm not sure if you can use the underscore instead of the slashes, I have never tried it before. if you do try I'd like to know the results =)
Good Luck
EDITED: ok, i was too curious about this issue and i wrote a route like this
Router::connect('/:company_medicine_:disease', array('controller' => 'pages', 'action' => 'test'),
array('pass'=>array('company','disease'),
'company'=>'[a-zA-Z]+',
'disease'=>'[a-zA-Z]+'));
and its not working u_U
as i suspected, the problem is that Cake thinks that the name of the custom route element is :company_medicine and not :company.. after a few minutes regarding/reading the code of Cake i found out the exact place where Cake parses the route and extracts the names of the passed elements. It's in /cake/libs/router.php in the class CakeRoute, method _writeRoute() (about line 1369):
preg_match_all('#:([A-Za-z0-9_-]+[A-Z0-9a-z])#', $parsed, $namedElements);
so as you can see in the regexp, the names of the elements can contain an underscore,therefore Cake thinks the name of the parameter is ":company_medicine".
So you have four solutions:
use slashes as separators for your urls
change the order of your parameter so it would be medicine_[COMPANY]_[DISEASE]
modify the line 1369 of the router.php to this (NOT RECOMENDED, i think it will break routes for plugins):
preg_match_all('#:([A-Za-z0-9]+)#', $parsed, $namedElements);
use url rewrite in your .htaccess to redirect all [COMPANY]_medicine_[DISEASE] to [COMPANY]/medicine/[DISEASE] so cake will see it separated by slashes and the browser will see it separated bu underscores. (I haven't tested it, i've never added another rule to the .htaccess for Cake =P)

How do you structure your URL routes?

Is there a specific pattern that developers generally follow? I never really gave it much thought before in my web applications, but the ASP.NET MVC routing engine pretty much forces you to at least take it into consideration.
So far I've liked the controller/action/index structure (e.g. Products/Edit/1), but I'm struggling with more complex urls.
For instance, let's say you have a page that lists all the products a user has in their account. How would you do it? Off the top of my head I can think of the following possibilities for a listing page and an edit page:
User/{user id}/Products/List, User/{user id}/Products/Edit/{product id}
User/{user id}/Products, User/{user id}/Products/{product id}
Products?UserID={user id}, Products/Edit/{product id}
I'm sure there are plenty of others that I'm missing. Any advice?
I like RESTful, user friendly and hackable URLs.
What does this mean? Lets start with user friendly URLs. To me a user friendly URL is something easy to type and easy to remember /Default.aspx?action=show&userID=140 doesn't meet any of these requirements. A URL like `/users/troethom´ seems logical though.
This leads to the next point. A hackable URL is an URL that the user can modify and still get presented with a result. If the URL is hackable and the URL for my profile is /users/troethom it would be safe to remove my user name to get a list of users (/users).
Using RESTful URLs is pretty similar to the ideas behind my other suggestions. You are designing URLs for a user and not for a machine and therefore the URL has to relate to the content and not the the technical back-end of your site. An URL as ´/users´ makes more sense than ´/users/list´ and an URL as ´/category/programming/javascript´ (representing the subcategory 'javascript' in the category 'programming' is better than ´/category/show/12´.
It is indeed more difficult to omit IDs, but in my world it is worth the effort.
Also consult the Understanding URIs section on W3C´s Common HTTP Implementation Problems. It has a list of common pitfalls when designing URIs. Another good resource is Resourceful Vs Hackable Search URLs.
You may want to take a look at the question "Friendly url scheme?".
Particularly, Larry.Smithmier's answer provided a list of common URL schemes when using MVC in ASP.NET.
Also, you may consider using different verbs to reuse the same routes for different actions. For example, a GET request to "Products/Edit/45" would display the product editor, whereas a POST to the same url would update the product. You can use the AcceptVerb attribute to accomplish this:
[AcceptVerb("GET")]
public ActionResult Edit(int id)
{
ViewData["Product"] = _products.Get(id);
return View();
}
[AcceptVerb("POST")]
public ActionResult Edit(int id, string title, string description)
{
_products.Update(id, title, description);
TempData["Message"] = "Changes saved successfully!";
return RedirectToAction("Edit", new { id });
}
Bill de hÓra wrote a very good essay entitled Web resource mapping criteria for frameworks that is well worth a read.
To add to troethom's comments, RESTful generally also means that, for example, to create a new user you would PUT a representation to /users/newusername
RESTful basically uses the 5 standard HTTP Methods (GET, PUT, POST, DELETE, HEAD) for controlling/accessing content.
Ok, this isn't easy for a web browser, but you can always use overloaded POST (post to /users/username with a representation of a user to change some of the details, etc.
Its a good way of doing things, I'd reccommend reading RESTFul Web services to get a better understanding :D (and it's a darn good book!)
I've seen two main accepted ways to approach this topic...
One is described in the MvcContrib project documentation
and the other one is described in a blog post by Stephen Walther (which I personally prefer).

Resources