Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
Like this article? We recommend

The RewriteRule Solution

To accomplish this expansion, you need to write a RewriteRule regular expression. First, you need to find the URI pattern /r/d, and then extract /d and turn it into /dhtml. Next, append a trailing slash.

Apache requires two directives to turn on and configure the mod_rewrite module: the RewriteEngine boolean and the RewriteRule directive. The RewriteRule is a regular expression that transforms one URI into another. The syntax is shown here:

RewriteRule <pattern> <rewrite as> 

So to create a rewrite rule to solve this problem, you need to add the following two mod_rewrite directives to your server configuration file (httpd.conf or .htaccess):

RewriteEngine  On
RewriteRule	^/r/d(.*)  /dhtml$1

This regular expression matches a URL that begins with /r/ (the ^ character at the beginning means to match from the beginning of the string). Following that pattern is d(.*), which matches one or more characters after the d.. Note that using /r/dodo would expand to /dhtmlodo, so you'll have to make sure anything after r/d always includes a /.

So when a request comes in for the URI <a href="/r/d/diner/">DHTML Diner</a>, this rule expands this abbreviated URI into <a href="/dhtml/diner/">DHTML Diner</a>.

  • + Share This
  • 🔖 Save To Your Account