Use Your Address Bar Like a Command Line

There have been many debates back and forth between the development communities as to how your URLs should look. 99% of the time, you are going to find that it is fueled by whatever Mr. Google says to do. That is just fine for ramping up your SEO results, but what about for your applications?

While I am not about to offer any new discipline or technique that you haven’t already heard before, I may offer way for you to approach a very common URL technique that is new to you. The good folks over at the Symfony Project offer, what I feel, to be a very logical method of creating the routes to your web application.

How many times have you seen a url like this …

…and how many times have you tried to change that number to look up a different user?

That is the theory behind using your address bar like a command line. There is no harm in guessing what value should be in there. We build URLs intuitively, and they should be used intuitively. Most people from the Linux community will agree, if we don’t have to use our mouse, we won’t. So if I see I can see how an application is creating it’s URLs, I will use this to my advantage, and it’s foolish to think that your visitors won’t if they are a little tech-savvy.

Take a look at this quote from a tutorial directly from Symfony:

In a web context, a URL is the unique identifier of a web resource. When you go to a URL, you ask the browser to fetch a resource identified by that URL. So, as the URL is the interface between the website and the user, it must convey some meaningful information about the resource it references. But “traditional” URLs do not really describe the resource, they expose the internal structure of the application. The user does not care that your website is developed with the PHP language or that the job has a certain identifier in the database. Exposing the internal workings of your application is also quite bad as far as security is concerned: What if the user tries to guess the URL for resources he does not have access to? Sure, the developer must secure them the proper way, but you’d better hide sensitive information

Couldn’t agree more.

How To Implement

There is no magic potion here, so this will be more of a resource for people looking to learn by example.

Let’s say your application is for movie theaters, and has one functionality of fetching a theater listing by state. A logic approach would be to build your URLs as[state abbreviation]. So if I live close to the border of New Jersey and New York (completely hypothetical, of couse) I can look up to find all of the theaters in New Jersey. Then, when I decided I’d rather go to the Imax theather in New York, I can switch it to . Sure there is a link on the page to select New York, but typing is just that much quicker –and that means improvements in “click”-thru’s.

Step one involves creating an httpd.conf (or .htaccess) directive to force any request to be routed to index.php.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Now when you try to fetch, it will still load index.php, but you can parse out th request URI to call an apporiate file and load specific data.

Request URI = theater-list/nj
$Page_Identifier = theater-list (maybe use it in a switch … and let’s say, call theater-list.php)
$Data_Identifier = nj

So now inside of index.php

require_once ($page_indentifier); // which is a file that we indentified using "theater-list"

Then, inside of theater-list.php, do something like

$sql = "SELECT * FROM theater-list WHERE state = '". $Data_Identifier . "' // of course, you will cleanse this data
  • Digg
  • Facebook
  • Google Bookmarks
  • Twitter

Comment | Trackback


Leave a Reply