What is a Rewrite Engine and what is it for?

Author: HOSTTEST Editorial   | 5 Sept 2019

Rewrite EngineThe content of a website is rarely static, but instead dynamically generated from an individual request. Similarly, it is undesirable for the corresponding address to have an incomprehensible format for humans - it usually consists of the domain, a subpage, and an additional code, often initiated with a question mark. One of the main tasks of a Rewrite Engine is therefore to pass generally readable information internally to the web server upon a request and to maintain an easily readable address externally, for example in the format domain.tld/subpage/content/topic.html. Furthermore, a Rewrite Engine can also handle more complex transformations or redirects.

Table of Contents

All popular webservers support a Rewrite Engine
How a Rewrite Engine works in detail
Practical use of Mod Rewrite in Apache 2 and NGINX
Apache 2 and the .htaccess file
Global configuration of the Rewrite Engine in NGINX
Various applications of a Rewrite Engine
Conclusion: Rewrite Engine facilitates SEO, provides flexibility, and additional security

All popular webservers support a Rewrite Engine

Until the mid-2000s, Apache 2 was by far the most widely used web server. In addition to its free availability due to its Open Source license, efficiency, and performance, the flexible use through its Rewrite Engine was a reason for its high popularity. Thanks to these advantages, the web server reached a market share of well over 80 percent at one point, with the only notable competition being Microsoft's IIS used for Windows Server.

Around 2005, alternatives to the Apache 2 server began to emerge, but they adopted and adapted the functionality of its Rewrite Engine. These include, for example, NGINX or the very lightweight and resource-efficient Lighttpd. It is certainly thanks to the example of Apache 2 that the concept of the Rewrite Engine could establish itself globally. Today, this extension is part of the core of all webservers used for the internet and has become an indispensable tool for webmasters and administrators in terms of user-friendliness and search engine optimization (SEO).

How a Rewrite Engine works in detail

To ensure high flexibility while keeping the core program as clear as possible, web servers today almost exclusively use a modular structure. Certain functions can be loaded separately and embedded into the basic setup as needed, similar to a plug-in. Following this concept, the Rewrite Engine is almost always an external module, known as Mod Rewrite (mod_rewrite) for example in Apache 2 and Microsoft IIS. To use it, the administrator must add Mod Rewrite to the main configuration - it will then be automatically loaded at each start or restart and available as a native function.

The operation of the Rewrite Engine is initially simple and understandable. The owner of a website defines specific locations or paths of a domain where Mod Rewrite becomes active. When the server receives a request for one of the relevant addresses, it forwards it to the engine, which rewrites the predetermined elements in a specified way and then forwards it. Different parameters can be used - the Rewrite Engine is capable of distinguishing requests based on addresses or metadata such as browser, IP address, client location, or link source. It can then select the request based on the desired features and redirect it to different target addresses.

However, the most common use case of a Rewrite Engine is undoubtedly rewriting addresses from an SEO- and user-friendly format to one required by the interpreting scripting language like PHP, Perl, or Python. To achieve this, Mod Rewrite converts parts of the URL or other available data into variables and passes them to the web server according to the specified pattern. It thus forms an internal abstraction layer between external and internal formats, ensuring compatibility between the two.

Practical Use of Mod Rewrite in Apache 2 and NGINX

Although every web server has adopted a Rewrite Engine into its concept, the practical application varies between different programs. This is partly due to the different software structures. Apache 2 consistently follows a modular principle, whereas NGINX compiles modules into the core program. As a result, these modules cannot be installed and loaded afterwards like in Apache 2, but must be considered during the package creation. This circumstance also leads to a different configuration of Mod Rewrite, causing settings between the two web servers to be incompatible.

Apache 2 and the .htaccess file

Almost every user of Apache 2 will sooner or later be confronted with the use of .htaccess files at different levels of the folder structure. With these, the web server offers a very clear and exceptionally flexible system to use the Rewrite Engine. The principle is very simple and is based on a file named .htaccess, which is created in one or more folders and contains instructions for Mod Rewrite. The process follows a strict, logically ordered hierarchy:

  • When a domain address is accessed, Apache 2 checks for the existence of a .htaccess file in the root directory.
  • If a corresponding file exists, the Mod Rewrite executes the instruction.
  • If no file exists, Apache 2 hierarchically checks all subsequent paths.
  • The web server passes each .htaccess file to Mod Rewrite until it reaches the target.
  • The Rewrite Engine processes redirections individually and processes them according to their order.
  • Subdirectories can override instructions from parent directories, including password protection, through explicit instructions.

For example, the last point is important for IT security - another task that is often handled by .htaccess. It allows for both a tiered access level and an independent differentiation between different areas of a website.

Global Configuration of the Rewrite Engine in NGINX

In contrast to Apache 2, NGINX does not allow for the local storage of rules within a directory, but defines them globally in its configuration. This makes it more difficult to use with some web hosts that do not allow user access to their own server. However, there is usually the option to use the Rewrite Engine through local settings. The crucial feature in any case is that all instructions are defined centrally and not by individual files within the respective directories.

Various Applications of a Rewrite Engine

The Rewrite Engine is often one of the most important functions for fine-tuning a web server. Its applications are practically unlimited and include the following scenarios:

  • Rewriting SEO-relevant addresses with relevant search terms
  • Creating a clear and memorable structure for directories
  • Differentiating between different levels
  • Securing specific areas through passwords, request origin, or IP range
  • Geoblocking specific regions
  • Setting up temporary or permanent redirects to another website

Conclusion: Rewrite Engine Facilitates SEO, Provides Flexibility, and Additional Security

For a web server, the Rewrite Engine is certainly one of the most important tools, handling numerous diverse tasks. Its universal approach simplifies many objectives that would otherwise require detours or extensive configuration. For this reason, it has become one of the most crucial aids for efficient management and for both temporary and permanent solutions in recent years. Its scope of tasks ranges from IT security to embedding relevant terms in SEO and to permanent redirects during a domain change. Therefore, the capabilities and potential of the Rewrite Engine should never be underestimated.

Photo: Pexels pixabay.com

Write a comment


More web hosts


More interesting articles

Set up and secure VPS Hosting

How do I best set up my VPS Hosting? This question is appearing more and more frequently in forums and blogs on the inte...

What does CGI-bin mean & what do I need it for

We'll show you what CGI-bin means and what you need it for on your website.