How to configure Apache rewrite rule-based request URL on Debian 12

To Configure Apache Rewrite Rule Based Request URL On Debian 12

Introduction :

Apache Rewrite rules are directives utilized in .htaccess files or virtual host configurations to alter the URL of a requested resource prior to server processing. These rules allow for URL modifications based on defined patterns, conditions, and actions.

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 : Go to the Document root directory by using following command.

root@linuxhelp:~# cd /var/www/

Step 3 : Long list the files by using following command.

root@linuxhelp:/var/www# ls -la
total 12
drwxr-xr-x  3 root     root     4096 Aug 19 18:19 .
drwxr-xr-x 13 root     root     4096 Aug 19 18:19 ..
drwxr-xr-x  4 www-data www-data 4096 Sep  5 08:03 html

Step 4 : Create a directory by using following command.

root@linuxhelp:/var/www# mkdir old-page

Step 5 : Create another directory by using following command.

root@linuxhelp:/var/www# mkdir new-page

Step 6 : Create a file inside first created directory by using following command.

root@linuxhelp:/var/www# touch old-page/old.html

Step 7 : Create another file inside second created directory by using following command.

root@linuxhelp:/var/www# touch new-page/new.html

Step 8 : Input some content for the first file by using following command.

root@linuxhelp:/var/www# echo "old html file content" >> old-page/old.html

Step 9 : Input some content for the second file by using following command.

root@linuxhelp:/var/www# echo "new html file content" >> new-page/new.html

Step 10 : Make Document Root location in virtual host.

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 11 : Restart Apache service by using following command.

root@linuxhelp:/var/www# systemctl restart apache2

Step 12 : Open firefox and search ip address with directory and file name

Step 13 : Create Dothtaccess 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 14 : Enable Apache rewrite module by using following command.

root@linuxhelp:/var/www# a2enmod rewrite
Module rewrite already enabled

Step 15 : Restart the Apache service by using following command.

root@linuxhelp:/var/www# systemctl restart apache2

Step 16 : Open Firefox and search IP address and direcory

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 Debian 12. Your feedback is much welcome.

FAQ
Q
How do I test my Apache Rewrite rules?
A
• Use a tool like curl or wget with the -v flag to inspect the rewritten URLs.
• Test your Rewrite rules directly in your web browser by visiting the desired URL.
Q
Can I use variables in my RewriteRule patterns and substitutions?
A
Yes! You can use Apache's built-in variables (e.g., %{REQUEST_URI}) in your RewriteRule patterns and substitutions.
Example:
RewriteRule ^/example/(.*)$ /new-path/$1 [R=301,L]
This will rewrite /example/old-url to /new-path/old-url.
Q
How do I block URLs with certain patterns using RewriteRule?
A
Use the [F] flag to forbid access to URLs that match the pattern.
RewriteRule ^/example/block-me$ - [F]
This will block all requests to /example/block-me.
Q
What is the difference between R=301 and R=302?
A
R=301 indicates a permanent redirect ( Moved Permanently), while R=302 indicates a temporary redirect. Choose carefully, as permanent redirects can have SEO implications!
Q
What is the syntax for writing RewriteRule directives?
A
RewriteRule [flags]
Here:
matches the URL against a regular expression.
specifies the new URL to use.
• [flags] optional flags can control the rewrite process.