How to Rewrite URLs with mod_rewrite for Apache on Ubuntu 20.04

How to Rewrite URLs with mod_rewrite for Apache on Ubuntu 20.04

Apache’s mod_rewrite module lets you rewrite URLs more cleanly, translating human-readable paths into code-friendly query strings. It also enables you to 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.

Introduction

Apache’s mod_rewrite module lets you rewrite URLs more cleanly, translating human-readable paths into code-friendly query strings. It also enables you to 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.

In this tutorial, you’ll enable mod_rewrite and use .htaccess files to create a basic URL redirection, and then explore a couple of advanced use cases.

Prerequisites

To follow this tutorial, you will need:

Step 1 — Enabling mod_rewrite

In order for Apache to understand rewrite rules, we first need to activate mod_rewrite. It’s already installed, but it’s disabled on a default Apache installation. Use the a2enmod command to enable the module:

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

mod_rewrite is now fully enabled. In the next step, we will set up an .htaccess file that we’ll use to define rewrite rules for redirects.

Step 2 — Setting Up .htaccess

An .htaccess file allows us to modify our rewrite rules without accessing server configuration files. For this reason, .htaccess is critical to your web application’s security. The period that precedes the filename ensures that the file is hidden.

apache urls ubuntu 20.04

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install Apache Maven on Ubuntu 20.04 | 18.04 |

This brief tutorial shows students and new users how to quickly and easily install Apache Maven on Ubuntu 20.04 | 18.04. Apache Maven comes with the default Ubuntu repositories… so all it takes is…

How to Install Apache Ant on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install Apache Ant on Ubuntu 20.04 | 18.04. Apache Ant is a Java library and tool that has number of built-in tasks allowing to compile, assemble, test and run Java applications. Apache Ant is a Java library and tool that has number of built-in tasks allowing to compile, assemble, test and run Java applications. If you’re...

How to Install Apache ActiveMQ on Ubuntu 20.04 | 18.04 |

This brief tutorial shows students and new users how to install Apache ActiveMQ on Ubuntu 20.04 | 18.04 server. For those who don’t know, Apache ActiveMQ is a free, multi-protocol, Java-based message…

How To Install Jenkins on Ubuntu 20.04 / Ubuntu 18.04

Jenkins is an open-source automation server that helps to automate the repetitive tasks involved in the software development process, which includes building, testing, and deployments. Jenkins was forked from the Oracle Hudson project and written in Java. Here, we will see how to install Jenkins on Ubuntu 20.04 / Ubuntu 18.04.

Install phpMyAdmin on Ubuntu 20.04 | 18.04 with Apache

Install phpMyAdmin on Ubuntu 20.04 | 18.04 with Apache - PhpMyAdmin is an open source web-based tool that allows users to easily manage MySQL or MariaDB…