How to configure Apache redirect rules based file extensions on Ubuntu 22.04
- 01:01 lsb_release -a
- 01:14 systemctl status apache2
- 01:32 vim /etc/apache2/apache2.conf
- 02:05 a2enmod rewrite
- 02:18 cd /var/www/html/
- 02:30 vim test.html
- 02:52 systemctl restart apache2
- 03:32 vim test.php
- 03:59 vim .htaccess
- 05:10 systemctl restart apache2
- 05:39 vim .htaccess
- 06:29 systemctl restart apache2
- 06:51 vim .htaccess
- 08:48 systemctl restart apache2
To Configure Apache Redirect Rules Based File Extensions On Ubuntu 22.04
Introduction:
The Apache mod_rewrite module allows for the clean rewriting of URLs, converting user-friendly paths into code-compatible query strings. Additionally, it provides the capability to rewrite URLs based on specific conditions. An .htaccess file facilitates the creation and implementation of rewrite rules without requiring access to server configuration files. By placing the .htaccess file in the root directory of your website, you can manage rewrites on an individual site or directory basis.
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: Then check the status of the Apache2 service
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 Mon 2024-09-09 02:37:00 IST; 42s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 3672 (apache2)
Tasks: 55 (limit: 4556)
Memory: 5.3M
CPU: 60ms
CGroup: /system.slice/apache2.service
├─3672 /usr/sbin/apache2 -k start
├─3673 /usr/sbin/apache2 -k start
└─3674 /usr/sbin/apache2 -k start
Sep 09 02:37:00 linuxhelp systemd[1]: Starting The Apache HTTP Server...
Sep 09 02:37:00 linuxhelp apachectl[3661]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. >
Sep 09 02:37:00 linuxhelp systemd[1]: Started The Apache HTTP Server.
Step –3: Edit Apache configuration file by using vim editor
root@linuxhelp:~# vim /etc/apache2/apache2.conf
change the following line
AllowOverride All
Step 4: Enable the Apache rewrite module by using following command.
root@linuxhelp:~# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
systemctl restart apache2
Step 5: Change the directory to Apache document root directory
root@linuxhelp:~# cd /var/www/html/
Step 6: Now create a simple HTML file using vim editor
root@linuxhelp:/var/www/html# vim test.html
HTML File
Step 7: Now restart the Apache service by using following command
root@linuxhelp:/var/www/html# systemctl restart apache2
Step 8: Open browser and search with the http://IP-Address/Filename.

Step 9: Again go to the terminal create simple PHP file using vim editor
root@linuxhelp:/var/www/html# vim test.php
Step 10: Create .htaccess file for Apache rewrite rules by using vim editor.
root@linuxhelp:/var/www/html# vim .htaccess
Add the following lines
RewriteEngine On
RewriteRule ^test.html$ test.php [L]
Step 11: Restart Apache service by using following command to apply changes.
root@linuxhelp:/var/www/html# systemctl restart apache2
Step 12: Now again open the browser and refresh the page

Step 13: Open .htaccess file and modify rewrite rule to redirect .html to .php file content.
root@linuxhelp:/var/www/html# vim .htaccess
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
Step 14: Restart the Apache service by using following command.
root@linuxhelp:/var/www/html# systemctl restart apache2
Step 15: Now again open the browser and refresh the page

Step 16: Open .htaccess file and modify rewrite rule to redirect file name to .php without file extension.
root@linuxhelp:/var/www/html# vim .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]
Step 17: Restart the Apache service by using following command to apply changes
root@linuxhelp:/var/www/html# systemctl restart apache2
Step 18: Now again open the browser and remove the file extension then load the page

Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to installation of Drupal CMS on Ubuntu 22.04. Your feedback is much welcome.
Comments ( 0 )
No comments available