How to configure Apache redirect rules based file extensions on Debian 12
- 00:46 cat /etc/os-release
- 00:58 vim /etc/apache2/apache2.conf
- 01:26 a2enmod rewrite
- 01:46 cd /var/www/html/
- 02:00 ls -la
- 02:06 vim test.html
- 02:31 systemctl restart apache2
- 03:13 vim test.php
- 03:43 vim .htaccess
- 04:45 systemctl restart apache2
- 05:19 vim .htaccess
- 06:13 systemctl restart apache2
- 06:40 vim .htaccess
- 09:10 systemctl restart apache2
To Configure Apache Redirect Rules Based File Extensions On Debian 12
Introduction:
The Apache mod_rewrite module provides the capability to establish intricate URL redirects through the use of regular expressions. This functionality enables the matching of various file extensions, including but not limited to .html, .php, and .css. By utilizing these advanced redirect rules, you can enhance your website's URL management, thereby facilitating improved navigation for both users and search engines.
Procedure :
Step 1: Check the OS version by using following command.
root@linuxhelp:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/
Step 2: Open Apache configuration file by using following command.
root@linuxhelp:~# vim /etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Step 3: Enable the Apache rewrite module by using following command.
root@linuxhelp:~# a2enmod rewrite
Module rewrite already enabled
Step 4: Go to the Apache document root directory location by using following command.
root@linuxhelp:~# cd /var/www/html/
Step 5: Longlist the files by using following command.
root@linuxhelp:/var/www/html# ll
total 4
-rw-r--r-- 1 root root 44 Aug 23 16:18 index.html
Step 6: Create a HTML file by using following command.
root@linuxhelp:/var/www/html# vim test.html
testing html file
Step 7: Restart the Apache service by using following command.
root@linuxhelp:/var/www/html# systemctl restart apache2
Step 8: Open firefox browser and search IP address with .html extension with file name.

Step 9: Create a PHP file by using following command.
root@linuxhelp:/var/www/html# vim test.php
testing php file
Step 10: Create .htaccess file for make apache rewrite rules by using following command.
root@linuxhelp:/var/www/html# vim .htaccess
RewriteEngine On
RewriteRule ^test.html$
Step 11: Open Firefox browser and refresh the page.

Step 12: 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 13: Restart the Apache service by using following command.
root@linuxhelp:/var/www/html# systemctl restart apache2
Step 14: Open Firefox browser and refresh the page.

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

Conclusion :
We have reached the end of this article. In this guide, we have walked you through the steps required to configure Apache redirect rules based file extension on Debian 12. Your feedback is much welcome.
Comments ( 0 )
No comments available