Information Resources
Apache Mod_Rewrite for Vanity URLs
Ever wondered how web services like Twitter (twitter.com/era404), Facebook (facebook.com/era404), and LinkedIn (linkedin.com/company/era404) are able to serve vanity URLs for their numerous users? The answer is in the rewrite.
Working with URL rewriting (namely: Apache’s mod_rewrite, a rule-based rewriting engine) can be a little bit confusing. LiquidWeb Hosting’s stellar technical support team passed the inquiry from tech-to-tech before a solution was found. This solution was further polished by ERA404 systems guru, Tony Muka, and is explained below.
What are the rules?
We mentioned “rule-based” when describing this Apache module. So what are the rules?
1. Ignore files that already exist on your web-space (index.htm, logo.png, and so on).
RewriteCond %{SCRIPT_FILENAME} !-f
2. Ignore directories that already exist on your webspace, too (image folders, script folders, and so on).
RewriteCond %{SCRIPT_FILENAME} !-d
3. For everything else, let’s pass anything after the base URL as an argument to your backend script to do whatever magic you wish to do. Now this is the confusing part because (unlike the previous two rules) it’s written in something that looks like Q*bert gibberish. Let’s have a look at that last rule.
RewriteRule ^([^/]+)/?(.*)$ /?user=$1&$2
Notice that this rule starts with “RewriteRule” and not “RewriteCond[ition]”. This is where we ask Apache to rewrite the URL if the first two conditions aren’t met—that is to say, it will only rewrite URLs for non-existent files and directories. Here’s a little breakdown of what will happen when you’ve put this instruction set on your web server:
Requested URL | domain.com/index.htm |
---|---|
Rewrites to | (no rewrite) |
Because | This file exists |
|
|
Requested URL | domain.com/images/ |
Rewrites to | (no rewrite) |
Because | This directory exists |
|
|
Requested URL | domain.com/username |
Rewrites to | domain.com/?user=username |
Because | No file or directory exists named “username” |
|
|
Requested URL | domain.com/username&action=profile |
Rewrites to | domain.com/?user=username&action=profile |
Because | No file or directory exists named “username” |
With this last requested URL, you can see the power of Apache’s mod_rewrite, because we can technically provide instruction for your handler scripts to do anything you need them to do. In this usage, a username is passed, as well as an action to present the profile page. This methodology can be used in countless scenarios where a long, cumbersome URL can be compacted, such as REST requests, XML requests, and so on.
Putting it All Together
# address translation for user URLs
RewriteEngine on
RewriteBase /
# look for url/username or url/username/ and translate to url/?user=username
# maintain all other passed arguments
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?(.*)$ /?user=$1&$2
Simply put this script into a text document, name it “.htaccess
” and drop it into your webserver’s root directory. Make sure that Apache’s mod_rewrite
module is activated on your HTTP server’s configuration (it usually is), and you’ll be all set to rewrite some URLs.
Permalink: https://www.era404.com/info/rewriting-urls/