The RewriteRule Directive in Apache

ยท

4 min read

Based on my personal experience, i gave reasons why you should use the .htaccess file and the necessary conditions for using it. You can check out this post to find out more

Today, i want to talk about how you can use this file to change the way URL's appear on your site, it is a technique i even recently used on a clients website. I would show a detailed example so that you can clearly understand what is going on.

One thing to note is whenever you want to write rules, you specify what are called DIRECTIVES. A directive indicates to the .htaccess file the rule you want it to follow when the file runs so that it knows what to do has your website is loading. Directives are case sensitive and must be written in a specific way.

The RewriteRule Directive

This directive powers the engine on Apache for handling all of your rewriting needs. This is how the syntax looks

RewriteRule Pattern Substitution [flags]

Lets use an example to see how the directive works

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Quick Overview

  • Before you can use the RewriteRule directive, you have to turn on the rewriting engine because it gives access to be able to rewrite URL's.

  • You can specify a condition that should occur before the RewriteRule directive fires off by using the RewriteCond directive. This directive acts like an if statement where you say if a is true then allow b to happen, you can also have more than one condition that must be met like in our example above. This directive though is not important as you can still use the RewriteRule directive without it.

  • At the last step, you then specify the RewriteRule that should be executed once any condition was met, it there were no conditions, it goes ahead and rewrites the url that matched the pattern you gave.

Lets break down the example much further

  1. Firstly, you turn on the rewrite engine.

  2. You specify the rewrite condition (If any). The syntax for this is RewriteCond TestString CondPattern [flags]

    • TestString is the parameter used to indicate the condition. In our example, TestString is %{REQUEST_FILENAME} which is a SERVER VARIABLE that searches for any file on the server that matches a url. The second TestString %{REQUEST_FILENAME}.php checks the server for only files that have the .php extension.
    • The CondPattern specifies what would make the TestString valid. In our example, !-d ensures we do not return a folder that has the same name with the URL. If we didn't use this flag, we run the risk of loading a folder that might match the URL instead of a file. -f on the other hand ensures that the server only searches for files that match the TestString.
    • Flags are optional parameters you can use. They are used for the TestString to specify certain edge cases like not case sensitive [NC] or force a redirect [R].
  3. Lastly, we specify the RewriteRule.

    • After indicating the directive, the next thing to indicate is your pattern. In our example, the pattern is ^(.*)$ which means match at the beginning of the url any type of value (number, string, symbol etc) 0 or more times and make sure the url ends with that value. For example, if the url was example.com/hello, it would look for any file on the server that starts and ends with the string "hello". It does not consider example.com because that is the location of the website.

    • Then the next thing that executes in the RewriteRule command is Substitution. Whenever a pattern is given, there is a parameter in the substitution command that instructs it to replace that parameter with whatever value matched the pattern in the url. In our example, $1 is the parameter, you could have more than one parameter ($2, $3, $4) but it depends on how many patterns you have defined. Assuming the server found a file with the name hello, the next step substitution takes is to replace $1 with hello which makes it hello.php. After doing that, the web server loads up hello.php using example.com/hello as the url. If the server could not find any hello.php page, it would show a 404 Not Found Error.

    • The two flags we specified simply say that the pattern checking should not be case sensitive and that the .htaccess file should stop the rewriting process immediately it has rewritten the url.

This is basically how you would rewrite urls with the RewriteRule directive. Sometimes it is even more complicated than this but so far as you understand the underlying principles, rewriting urls should not be a problem. Hope you found this useful, you can connect with me on Twitter and Linkedin.