How to Configure mod_rewrite?

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"

Hello, friends. One of the most recommended settings if you have a personal blog on your server is to make the web addresses understandable to novice users. This will make it well weighted by search engines. Therefore, today, you will learn how to configure mod_rewrite on Linux. Let’s see.


The mod_rewrite module is an Apache web server module that rewrites or redirects requests to specified content. It allows you to create alternative URLs to the dynamic ones generated by the programming of your website.

The use of the rewrite mod is critical for SEO because search engines prioritize addresses understandable to the user and not regular expressions.

Let’s go for it.

Install Apache on Linux

As it is a module of Apache, we have to use this web server. The installation on each Linux distribution is not difficult, but differs a little bit. I will use Debian / Ubuntu as an example.

First, update the system:

sudo apt update
sudo apt upgrade

And then install Apache

sudo apt install apache2

Now enable ports 80 and 443 in the Firewall.

sudo ufw allow 80
sudo ufw allow 443

And if you want to check the status of the firewall

sudo systemctl status apache2

Now we can continue.

Configure mod_rewrite

The first thing we have to do is to enable the module in Apache. To achieve this, just run.

sudo a2enmod rewrite
Enable and configure mod_rewrite
Enable and configure mod_rewrite

To apply the changes, you can restart the system

sudo systemctl restart apache2

The next step is to create a .htaccess file for the website that we want to apply the configuration. Remember that .htaccess files modify site-specific settings and do not affect other sites or global settings. Therefore, you will have to save it in the directory of the website to apply the configuration. For this post, I will use the default path.

sudo touch /var/www/.htaccess

Now we will have to modify the Apache configuration file to indicate that .htaccess will overwrite the configuration.

sudo nano /etc/apache2/apache2.conf

Check this section

<Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted

Replace AllowOverride None with AllowOverride All and it will be as follows

<Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
Enable htaccess on Apache configuration
Enable htaccess on Apache configuration

Of course, save the changes and close the editor. To apply them, you just need to restart Apache

sudo systemctl restart apache2

Using mod_rewrite

Where the magic of mod_rewrite happens is the .htaccess file, where the first thing we need to know is how to enable it on the site. To achieve this, simply add.

RewriteEngine On

Or to disable it

RewriteEngine Off

From there, then you have to apply each of the configurations that you consider appropriate for the website. They can be conditions, rules or engines.

A basic example of how to use mod_rewrite is to redirect from one page to another page to another. To achieve this, use this in the .htaccess.

RewriteEngine On
RewriteRule ^oldsite.html$ newsite.html [NC]

First, you enable the rewrite. And in the second line it is indicated that when accessing oldsite.html it will be redirected to newsite.html [NC] implies that it is not case-sensitive.

The result of applying this rule (you must restart Apache to do this) is that when a user tries to access oldsite.html they will be redirected to newsite.html.

Another common example is that it allows us to transform parameterized addresses to more readable ones.

For example, a user gets the following address

But at the request level, what is sent to the server is as follows

To obtain this result, you have to set these rules

RewriteEngine On
RewriteRule ^posts/([A-Za-z]+)/([A-Za-z]+)$ request.php?id=$1&name=$2

Here we have to stop a bit. After posts/ follow some regular expressions that together will transform id=1 into sample and the second expressions change name=$2 to debian.

But as you can see, the possibilities are almost endless.


This post served as a simple introduction to mod_rewrite, this Apache tool that can rewrite addresses almost infinitely. I hope you liked it.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.


Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook