Mod_rewrite is a module for the Apache server. This module adds new functionalities to the server to manipulate URLs and requests that come to the server. All URLs can be verified and validated by regular expressions or conditions specified by the programmer If the URL structure meets certain conditions, you can redirect to a specific resource on the server or redirect to an error page.
These functionalities are vital to a PHP framework to work at full capacity.
The most common reasons why mod_rewrite is used are:
1. Memorizing the URLs used;
2. The aesthetics of the URLs;
3. Better indexing by search engines;
4. Transparent user redirection from one page to another.
Like any Apache module, mod_rewrite is enabled from the global configuration file (httpd.conf).
You have to check if the row in this file is uncommented (remove the # from the beginning of the row) which specifies how the mode is loaded.
Example:
From: #LoadModule rewrite_module modules/mod\_rewrite.so
To: LoadModule rewrite_module modules/mod\_rewrite.so
In newer versions of Apache, mod_rewrite is enabled by default. This means that at a web hosting provider where you want to run a website and you do not have access to the httpd.conf file, mod_rewrite should be enabled by default. If the module is disabled – contact the administrator to enable it. The easiest way to check if mod_rewrite is enabled is to create a test.php file with the following content: <?php phpinfo(); ?> The phpinfo () function displays the configuration of the Apache server that you want the application to run on.
The Apache server has some configuration commands called directives. Any newly installed Apache module offers new configuration directives. Once you’ve installed it and made sure the mod_rewrite module works, you can use its directives to rewrite URLs.
These configurations must be put into a file in each application folder where you need them to be valid.
The default name of this file that the Apache server searches for uploading is called .htaccess
The settings written in .htaccess in the root folder will also apply to the “items” folder. If I do not want this to happen, a .htaccess must also be created in the “articles” folder.
The easiest way to check if the directives you write in .htaccess are loaded by Apache, is to write a “invalid” character string to it and access the URL where you created the file.
The basic directives available in mod_rewrite that we need and which we will write in .htaccess in order to rewrite the URLs are:
• RewriteEngine – controls whether URL rewriting is done, and has only one parameter that can be on or off;
• RewriteBase – tells the rewrite engine which is the relative path it rewrites; this path is extracted from the URL, and it is added once the rewrite rules have been evaluated. It has one parameter, which is a relative path to the root document, for example RewriteBase /
• RewriteCond – can be used to condition the evaluation of the next RewriteRule directive. It has 3 parameters:
1. The value to be tested (we are only interested in %{REQUEST_FILENAME}, the complete list is given in the official mod_rewrite documentation);
2. The test itself, the tests that we are interested in are -f and -d;
3. An optional flag that can be NC (nocase) or OR (ornext).
Typically, we use RewriteCond for checking whether the requested address is a folder (-f) or a directory (-d) on the hard disk, and if so, we do not have to rewrite the URL. The [L] flag indicates that the rewrite engine stops after rewriting and stops executing the next commands in .htaccess.
• RewriteRule – the most important directive, does the actual rewrite. It has 3 parameters, like RewriteCond:
1. The rewriting URL;
2. The address with which to replace it;
3. Flags.