views:

214

answers:

8

I've seen a lot of URIs that look something like this:

www.fakesite.net/stories/1234/man_invents_fire

and I was wondering if the /1234/man_invents_fire part of the URI are actually directories or if they are GET parameters (or something else). I've noticed that a lot of times the /man_invents_fire segment is unnecessary (can be removed with no consequences), which led me to believe that the /1234/ is the id number for the story in a database table (or something along those lines).

If those segments of the URI are GET parameters, is there an easy way of achieving this? If they aren't, what is being done?

(also, I am aware that CodeIgnitor gives this kind of functionality, but I was curious to find out if it could be easily achieved without CodeIgnitor. I am, however, generally PHP, if that is relevant to an answer)

Thanks

A: 

That is called url rewriting, google for it, you will find a lot of information about that.

Cleiton
A: 

Just like permalinks in WordPress, this is done typically done via Apache's mod_rewrite (or an equivalent thereof if not using Apache); however, you can also use a 404 error page handler to achieve the same result (but this is not usually recommended).

Typically, all page requests are redirected to a gateway that parses the requested URI to determine if it fits the specified pattern (in your case likely to be /{category}/{article_id}/{article_title}). From there, the gateway can typically use just the article_id to retrieve the appropriate content.

Depending on the system, category and article_title can usually be thrown away/ignored and are typically for SEO value; however, in some cases category might be used to augment article_id in some way (e.g.: to determine what DB table to query, etc).

MVC's, like Zend, also use a similar technique to determine which controller and method therein to execute. An example format for this type of use is /{module}/{controller}/{method}; however, this is highly customizable.

Justin Johnson
A: 

If you are using Symfony, then you can use the routing feature to do this.

Ngu Soon Hui
I have changed my answer.
Ngu Soon Hui
i think he wants a solution for php... but true, in asp.net world, MVC makes it a lot easier...
Sander Versluys
A: 

Well, you are kind of right in assuming that the 1234 and *main_invents_fire* are parameters. They are not truly GET parameters in the sense that the HTTP protocol describes them but they accomplish the same task, while keeping the URL "friendly". The technique is called URL rewriting and the web is full of info on this these days..

Here's an article about friendly URLs in PHP but I'm sure googling for the topic will render more useful results.

Miky Dinescu
A: 

Implementing this in PHP is typically done via an .htaccess file and using apache's mod_rewrite module.

Jonathan Sampson
A: 

They make the url like that so that people can easily bookmark it, and it can return safely in the search.

Depends on what language you're using to decode it. In this case, it appears "stories" is the main script, and "1234" is the id, and "man_invent_fires" is the title.

If you're using php, you can use the $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] variable to decode it.

If you're planning to make a website like that, certain safety must be kept in mind. Look them up in google, but key one to look out for is sql injectors.

Daniel
This typically has very little to do with bookmarking and is generally used as an SEO technique.
Justin Johnson
+1  A: 

Easiest thing to do is route everything into a main index.php file and figure out your routing from there by running $pieces = explode("/", $_SERVER['REQUEST_URI']);

After installing/enabling mod_rewrite, make sure allow override is not set to false in your apache config (to allow .htaccess to be read), then throw this in your docroot's .htaccess file.

<ifModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-s #Make sure the file doesn't actually exist (needed for not re-routing things like /images/header.jpg)
    RewriteRule .  /index.php  [L,QSA] #re-route everything into index.php
</IfModule>
Derek Gathright
A: 

As some background information in addition to the answers before me, a URL is just that - a 'Uniform Resource Locator'. Although in the old days, it often used to map 1:1 to a file/directory structure, all that is required in the HTTP spec is to identify a certain resource. Basically that means that, given a certain string, it should indicate a certain 'resource' on the server. In practice, in a HTTP environment this is usually implemented with a rewriting mechanism such as mod_rewrite. RFC's such as this one: http://www.ietf.org/rfc/rfc1738.txt give a nice, albeit abstract, overview. The concepts only come to life after designing and implementing some non-obvious uses, though.

Roel
Also, see this: http://tomayko.com/writings/rest-to-my-wife .
Roel