Common htaccess Rules

Regular Expressions

Rewrite rules often contain symbols that make a regular expression (regex). This is how the server knows exactly how you want your URL changed. However, regular expressions can be tricky to decipher at first glance. Here’s some common elements you will see in your rewrite rules, along with some specific examples.

  • ^ begins the line to match.
  • $ ends the line to match.
    • So, ^folder1$ matches folder1 exactly.
  • . stands for “any non-whitespace character” (example: a, B, 3).
  • * means that the previous character can be matched zero or more times.
    • So, ^uploads.*$ matches uploads2009, uploads2010, etc.
  • ^.*$ means “match anything and everything.” This is useful if you don’t know what your users might type for the URL.
  • () designates which portion to preserve for use again in the $1 variable in the second string. This is useful for handling requests for particular files that should be the same in the old and new versions of the URL.

How to check your htaccess

Before uploading you new htaccess to the live server, you can test it by using this online htaccess tester.

Learning Resources