How to configure Apache rewrite rule-based query string on Debian 12

To Configure Apache Rewrite Rule Based Query String On Debian 12

Introduction:

Apache's mod_rewrite module is a robust and versatile tool for URL modification. It enables web administrators to alter URLs based on a range of conditions, including query strings—segments of the URL that follow the "?" character and are commonly utilized for passing parameters to web applications.

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 52
drwxr-xr-x  7 www-data www-data 4096 Sep  6 03:35 .
drwxr-xr-x 13 root     root     4096 Aug 19 18:19 ..
-rw-r--r--  1 www-data www-data   14 Sep  6 01:51 1
-rw-r--r--  1 www-data www-data  128 Sep  6 03:33 .htaccess
drwxr-xr-x  4 www-data www-data 4096 Sep  5 08:03 html
drwxr-xr-x  2 www-data www-data 4096 Sep  6 03:21 live
-rw-r--r--  1 www-data www-data   18 Sep  6 03:13 live.html
drwxr-xr-x  2 www-data www-data 4096 Sep  6 03:21 local
-rw-r--r--  1 www-data www-data   19 Sep  6 03:13 local.html
drwxr-xr-x  2 www-data www-data 4096 Sep  5 08:10 new-page
-rw-r--r--  1 www-data www-data   14 Sep  6 01:51 new-page.html
drwxr-xr-x  2 www-data www-data 4096 Sep  5 08:10 old-page
-rw-r--r--  1 www-data www-data   14 Sep  6 01:51 old-page.html

Step 4: Create a file with content by using following command.

root@linuxhelp:/var/www# vim old.html
old html file content

Step 5: Create another file with content by using following command.

root@linuxhelp:/var/www# vim new.html
new html file content

Step 6: Create rewrite rule in apache virtual host by using following command.

root@linuxhelp:/var/www# vim /etc/apache2/sites-available/000-default.conf 
DocumentRoot /var/www/
<Directory /var/www>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^parameter1=value1$
RewriteRule ^old\.html$ /new.html? [R=301,l]
</Directory>

Step 7: Enable apache rewrite module.

root@linuxhelp:/var/www# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2

Step 8: Restart apache service

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

Step 9: Open firefox and search ip address with old filename with Query string.

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

FAQ
Q
5. What should I do if my rewrite rules are not working?
A
If rewrite rules are not working, check the following:
• Ensure mod_rewrite is enabled and properly loaded.
• Verify that the rewrite rules are correctly written and placed in the right configuration context.
• Look for syntax errors in the Apache configuration or .htaccess file.
• Check Apache’s error logs for any errors related to rewrite rules.
Q
4. What are the flags used in rewrite rules?
A
• R: Redirects the request. The R flag can be followed by an HTTP status code, such as R=301 for a permanent redirect.
• L: Last rule. Stops processing further rules if the rule matches.
• QSA: Query String Append. Appends the original query string to the target URL if the target URL has its own query string.
Q
3. Where should I place rewrite rules?
A
Rewrite rules can be placed in various locations depending on your needs:
• .htaccess Files: Useful for per-directory rules and is often used in shared hosting environments.
• Apache Configuration Files: Placing rules in or blocks within Apache's configuration files is more efficient for performance and centralized management.
Q
2. How do rewrite rules based on query strings work?
A
Rewrite rules based on query strings use the RewriteCond directive to check if the query string matches a specific pattern. If the condition is met, the RewriteRule directive applies a specified rule to rewrite or redirect the URL.
Q
1. What is mod_rewrite in Apache?
A
mod_rewrite is an Apache module that provides a powerful and flexible way to perform URL rewriting. It allows web administrators to rewrite URLs based on various conditions such as query strings, headers, and more.