Understanding .htaccess, Regular Expressions, and URL Rewrite Rules, with Examples

The .htaccess file has probably cost unexperienced and experienced web developers more time than any other file on the internet. But understanding it has a lot of benefits – improving SEO, usability, workflow, and your health (too much swearing is hard on your morale!).

The problem is that very few articles you find actually give you the tools you need to troubleshoot and understand what's going on in .htaccess, and instead give you step by step solutions to a specific problem in a specific environment, which nearly always, is not exactly your problem and is slightly different than your environment.

So let's take a step back and look at what .htaccess is doing, how it does it, and examine a few examples of how it works. (Bear with me designers, we need to take a short detour here to talk about our environement and we'll get back to how this relates to the web page in moment!)

At it simplest, .htaccess is a config file. It holds settings and rules that help your webserver behave the way you want it to. It's not the main config file on your webserver but frequently, it is the only config file that you have access to. The priority of the settings on your Apache web server are as follows:


Apache is your web server software. Your hosting company typically sets this up for you, but if you set this up yourself, you would learn that you add all of your settings and preferences to a file called httpd.conf


On top of Apache, you install PHP. PHP installs all the files needed for you to write code in the PHP programming language and when your PHP files are sent to your web server (Apache), Apache knows to have PHP process them in order to know what HTML to give back for your web page. PHP also has a config file that lets you set a variety of rules and preferences on how it runs too. This file is called php.ini. Rules set in php.ini can complement or override rules set in apache (I should probably check on that statement)

Website Rules

Finally, we have your website. Each time you load a page it needs to talk to your Apache server and the successful pages you load or error messages you receive are responses determined by the rules you have set up in your Apache config file (httpd.conf) and your PHP config file (php.ini). While many folks who work on websites don't want to learn all of the deeper server level stuff, often, they still like tweaking some of the settings, and that is what we use .htaccess for.

.htaccess allows you to override some rules set by your web server or PHP installation, as well as create some new rules so your website behaves in the way you want it to.

So, how the heck do we add rules to the .htaccess. file? You're probably familiar with various htaccess rules that use regular expressions and make fun all the silly characters. If you're like most people, you troubleshoot what you're trying to do by changing a character or two, saving, and refreshing the page with your fingers crossed hoping things will improve and you can escape this miserable affair with .htaccess. But enough of that. It's time to conquer the beast, understand what it's doing, and move on. Here's how you do it.

Understanding .htaccess involves 3 things:

Environmental Variables

In order to make rules using htaccess you generally need to modify an existing variable in your environment and change it to a new one. Understanding what these variables are and where you can find them is step one.

Open up a PHP page on your server and drop this snippet of code in your file.

  echo "<pre>";
  echo print_r($_SERVER);
  echo "</pre>";

This snippet of code is going to output an array of global variables you have access to. An abbreviated version of your array looks like this:

  [SERVER_NAME] => www.barrelstrengthdesign.com
  [DOCUMENT_ROOT] => /Path/To/The/Root/Directory/On/Your/Server

In PHP, you can access these variables by using sessions:


In .htaccess, we can access the same variable using a different syntax:


Both of these variables, in PHP and .htaccess, equal the value "www.barrelstrengthdesign.com" which we can see in our the global array that we output in the first step.

This post will be expanded to include more information on Regular Expressions, htaccess rules, and examples.