Image Image Image Image Image
Scroll to Top

To Top




Information Resources


By admin404

Apache Mod_Rewrite for Vanity URLs

March 5, 2013 - Information Resources

Ever wondered how web services like Twitter (, Facebook (, and MySpace (currently: 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:

This Requested URL Rewrites to Because (no rewrite) …this file actually exists (no rewrite) …this directory actually exists No file or directory exists
named “username”
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.


Tags | .htaccess, apache, engine, mod, request_filename, REST, rewrite, rewritecond, rewriteengine, rewriterule, script_filename, uniform resource locator, url, xml