I’ve been to so many sites recently that serve me URLs like this one:
http://example.com/index.php?act=search&query=some%20search
%20query&sessionid=5ef67f1752fe7496
Frankly, I am sick of these kinds of sites. This article digs a bit deeper into why this is bad, and offers solutions for webmasters that do have URLs like the one above.
Why Long URLs Are Bad
Other than the obvious reasons, there are some deeper justifications. Let’s jump right in..
Aesthetics
I (and probably many other computer users) are annoyed by long and incomprehensible URLs. Instead, they should look good by being simple and short.
Readability
By reading your site’s URLs, I should be able to understand what would happen if I visited that page. This is a bad example of URL readability:
http://example.com/actions/?1
I am able to understand by reading the URL that some sort of action will be performed, but the ?1 gives no clue as to what it is. This is a much better URL:
http://example.com/actions/send/
Now it is perceptible that something will be sent. But still, there is no hint of what will be sent. This is an even-better URL:
http://example.com/mail/send/
The user can now understand that by visiting this URL, mail will be sent.
Memorable URLs
The average person could never memorize a URL like the one at the top of this page—that is what the Clipboard is for. Your URLs need to be short and sweet—short enough so that it is easy to remember.
Now, that doesn’t mean you can’t have URLs that are a bit long. Even when using folders for URLs as in the examples above, these links can get a bit long. But so long as the URL makes sense, it will still be memorable. For instance, this long URL is, well, long—but it is still easy to remember because the link is hierarchically sorted.
http://example.com/mail/send/somecompany/jerry/
Solutions for Shorter URLs
Despite the problematic natures of long, hard-to-remember URLs, they can always be easily shortened. Here are some solutions and methods of doing so.
Send POST Data Instead of GET
For those who don’t know the difference, GET data is included in the URL, while POST data is not visible. By using POST data instead of GET data, the first URL shown in this post could be shortened to:
http://example.com/index.php
All of the ugly GET data is now instead being passed via POST, so the URL is much shorter already. But this URL does still not accord to the rule of readability from earlier in this post. Its purpose should be clearly visible, so let’s change the URL.
http://example.com/search.php
POST solves a lot of our URL-length problems, but there are still the problems of readability, aesthetics, and memorability.
Use Apache’s .htaccess To Simulate Directories
In the earlier example of memorability, we finished with this URL:
http://example.com/mail/send/somecompany/jerry/
However, the only real folder in this URL should be the /mail folder. The rest of the folders can be simulated with a file called .htaccess, a special type of per-directory configuration file for Apache servers.
In the .htaccess we can redirect that URL to this new, server-readable link.
http://example.com/mail/?act=send&company=somecompany&person=jerry
In this way, we can simply place one index file in the /mail directory to perform the actions.
You might be wondering why I would suggest that URL, as it breaks all the rules of URL readability, aesthetics, and memorability. But actually, this URL is never shown to the user. Although the server is told to serve the above URL, we can make sure the user only sees the clean and short one with our .htaccess file.
This amazingly simple, yet powerful technology is called rewriting. Rewriting requires the mod_rewrite module to be turned on (by default it is enabled). The rewrite module uses regular expressions to match URLs, so this allows the URLs to be very flexible.
Here is an example of an .htaccess file that we could place in the /mail directory, that would perform what was described four paragraphs ago.
# Turn the rewriting engine on RewriteEngine on # Should be used RewriteOptions inherit # Base directory RewriteBase /mail/ # Rule for rewriting RewriteRule ^(.+?)/(.+?)/(.+?)(/|)$ index.php?act=$1&company=$2&person=$3
This is only an example—there are many more things you can do with rewriting.
Don’t Show Filetypes or Filenames
Most non-literate computer users (who are a large portion of the market) will be confused about or forget endings like .php, .html, .htm, and others. It is much better to show folder names instead of specific files. For example, this URL:
http://example.com/search.php
Should be changed to this URL.
http://example.com/search/
If you don’t want to create folders for every action, you could just keep search.php and use the .htaccess method mentioned above this to serve search.php when the /search folder is visited.
Fin
Thanks for reading! Hopefully these tips will help improve certain (*cough*) websites in the near future.
you should never use POST just to get prettier urls. POST has a very specific functional meaning. it is intended for links that cause permanent actions (making a purchase, updating user data in db) and should not be “passed around” by users. most browsers will warn the user upon resubmitting a POST, for example.
if you use POST for things like performing a search, you are doing a disservice to your users who might want to pass around links to each other, to show each other search results pages.