What is mod_rewrite and how to setup mod_rewrite for Apache?

Steps to to setup mod_rewrite for Apache.

  1. Bhawani Singh
    Mod_rewrite is considered to be one of the most powerful, yet troubling subject. Most often people are confused and frustrated by the complexity of mod_rewrite. People even believe that it is quite a task to master the subject!

    Don’t get frustrated! We are here to clear the basics of the subject; and to master it you need to practice on your own!

    A Brief on the mod_rewrite:

    mod_rewrite is a module in Apache which enables server-side handling or modification of requested URLs.

    If you are wondering how it works? It checks the inward URLs based on asset of rules. These rules comprise of a normal expression in order to identify a precise pattern. If the pattern is seen in the URL, and the stipulated conditions meet, it is replaced by a given alternative string. The advantage of mod_rewrite is that it transforms cryptic and tough URLs into user friendly and decipherable ones.

    This process carries on until there are no rules left to be met or the process is clearly ordered to stop.

    The entire concept of mod_rewrite can be summarised in following points:

    • There is a set of rules that are administered in a particular order.
    • If a rule meets the stipulation, it checks the environments for that rule.
    • If the conditions are met, a replacement or substitution is done.
    Enabling mod_rewrite on the Server

    The first and foremost thing that you should keep in mind that enabling of mod_rewrite or any other apache module as a matter of fact has to be done on the global configuration file (same as enabling .htaccess module), which is “httpd.conf”.

    Since the usage of mod_rewrite is so common and prevailing, most of the hosting companies keep it enabled almost all the time.

    If you have done the Apache installation, it has to be noted that mod_rewrite requires being included when it isbrought together, as it is not included by default. Nevertheless, it issuch a common thing that almost all the installation guides show the process.

    If you are the administrator of your server, and you need to ensure that you run the correct module, you must look in the httpd.conf file. In this configuration file, you will notice that there is a large unitthat loads a set of modules. The following line will appear at a place within the file. And you should be happy as you have done it successfully!

    However, if it is mention commented out, which means there is a # symbol at the beginning of the line, you can easily uncomment it and for that just remove the # symbol:

    Code:
    LoadModule rewrite_module modules/mod\_rewrite.so
    The older version of the Apache 1.3 module may need you to add the below mention instruction after the LoadModule instruction:

    Code:
    # Only in Apache 1.3 AddModule mod\_rewrite.c
    However, this has been eliminated from the module in Apache 2 and above, wherein only the LoadModule instruction is required.

    In case you haveto alter the configuration file completely, which of course is a rare occurrence; you will be required to re-start the web server. Like how it is supposed to be done always, you must not forget to create and keep a back-up of the original file for a future reference.