How to enable Rewrite URLs with mod_rewrite for Apache on Ubuntu 20.04

 Introduction

Apache’s mod_rewrite module lets you rewrite URLs in a cleaner fashion, translating human-readable paths into code-friendly query strings. It also lets you rewrite URLs based on conditions.

An .htaccess file lets you create and apply rewrite rules without accessing server configuration files. By placing the .htaccess file in the root of your web site, you can manage rewrites on a per-site or per-directory basis

Prerequisites

To follow this tutorial, you will need:

  • One Ubuntu 18.04 server set up by following the Ubuntu 18.04 initial server setup guide. This includes a sudo non-root user and a firewall.

  • Apache installed by following Step 1 of How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 20.04.

     

Step 1 — Enabling mod_rewrite

       #sudo a2enmod rewrite

This will activate the module or alert you that the module is already enabled. To put these changes into effect, restart Apache.

            # sudo systemctl restart apache2

Step 2 — Setting Up .htaccess

           #sudo nano /etc/apache2/sites-available/000-default.conf

By default, Apache prohibits using an .htaccess file to apply rewrite rules, so first you need to allow changes to the file. Open the default Apache configuration file using nano or your favorite text editor.

<VirtualHost *:80>
    <Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    . . .
</VirtualHost>
 

# sudo systemctl restart apache2

 

 

 

 

Comments