How to configure Apache rewrite rule-based request URL on Ubuntu 22.04
- 01:00 lsb_release -a
- 01:14 systemctl status apache2
- 01:24 cd /var/www/
- 01:29 ll
- 01:39 mkdir old-page
- 01:48 mkdir new-page
- 02:10 touch old-page/old.html
- 02:18 touch new-page/new.html
- 02:41 echo "Old HTML File" >> old-page/old.html
- 02:57 echo "New HTML File" >> new-page/new.html
- 03:14 vim /etc/apache2/sites-available/000-default.conf
- 04:00 systemctl restart apache2
- 04:43 vim .htaccess
- 06:03 a2enmod rewrite
- 06:13 systemctl restart apache2
To Configure Apache Rewrite Rule Based Request URL On Ubuntu 22.04
Intoduction
The Apache HTTP Server, frequently referred to as Apache, is a highly effective and versatile web server software utilized for delivering web content. A key feature of Apache is its capability to manage URLs and handle requests through rewrite rules facilitated by the mod_rewrite module. This module provides a powerful means to dynamically alter request URLs based on defined patterns, resulting in cleaner URLs, an enhanced user experience, and improved search engine optimization (SEO).
Procedure:
Step – 1: Check the OS version
root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
Step – 2: Check the status of the Apache2 service by using the following command
root@linuxhelp:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2024-09-11 04:27:08 IST; 9min ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 2826 (apache2)
Tasks: 55 (limit: 4556)
Memory: 5.3M
CPU: 87ms
CGroup: /system.slice/apache2.service
├─2826 /usr/sbin/apache2 -k start
├─2831 /usr/sbin/apache2 -k start
└─2832 /usr/sbin/apache2 -k start
Sep 11 04:27:08 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Sep 11 04:27:08 linuxhelp apachectl[2822]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1>
Sep 11 04:27:08 linuxhelp systemd[1]: Started The Apache HTTP Server.
Step – 3: Change directory to Apache document root directory
root@linuxhelp:~# cd /var/www/
Step – 4: List the directory
root@linuxhelp:/var/www# ll
total 12
drwxr-xr-x 3 root root 4096 Sep 11 04:27 ./
drwxr-xr-x 15 root root 4096 Sep 11 04:27 ../
drwxr-xr-x 2 root root 4096 Sep 11 04:27 html/
Step – 5: Now create two directories
root@linuxhelp:/var/www# mkdir old-page
root@linuxhelp:/var/www# mkdir new-page
Step – 6: Then create one HTML file in each directories
root@linuxhelp:/var/www# touch old-page/old.html
root@linuxhelp:/var/www# touch new-page/new.html
Step – 7: Add some text to the each files.
root@linuxhelp:/var/www# echo "Old HTML File" >> old-page/old.html
root@linuxhelp:/var/www# echo "New HTML File" >> new-page/new.html
Step – 8: Now open default virtual host file and change the path
root@linuxhelp:/var/www# vim /etc/apache2/sites-available/000-default.conf
DocumentRoot /var/www/
<Directory /var/www/>
AllowOverride All
Require all granted
</Directory>
Step – 9: Restart the Apache2 service by using following command
root@linuxhelp:/var/www# systemctl restart apache2
Step – 10: Open the browser and search the IP with old file path

Step – 11: Now again go to the terminal and create .htaccess file to create rewrite rule by using following command.
root@linuxhelp:/var/www# vim .htaccess
RewriteEngine On
RewriteRule ^old-page/(.*)$ /var/www/new-page/$1 [L]
Step – 12: Enable Apache rewrite module by using following command.
root@linuxhelp:/var/www# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
Step – 13: Restart Apache2 service by using following command
root@linuxhelp:/var/www# systemctl restart apache2
Step – 14: Go to the browser and remove the old.html file then reload the page. It will show the new.html file.

Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to configure Apache rewrite rule based request URL on Ubuntu 22.04. Your feedback is much welcome.
Comments ( 0 )
No comments available