How to configure Apache rewrite rule based query string on Ubuntu 22.04

To Configure Apache Rewrite Rule Based Query String On Ubuntu 22.04

Introduction:

The Apache mod_rewrite module is a powerful and flexible tool for modifying URLs. It allows web administrators to adjust URLs based on various conditions, including query strings, which are the segments of a URL following the "?" character and are typically used for passing parameters to web applications.

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 : Go to the Apache document root directory

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

Step – 3 : Now list the directory

root@linuxhelp:/var/www# ls -la
total 12
drwxr-xr-x  3 root root 4096 Oct 29 04:06 .
drwxr-xr-x 15 root root 4096 Oct 29 03:57 ..
drwxr-xr-x  2 root root 4096 Oct 29 03:57 html

Step – 4 : Create a file using vim editor

root@linuxhelp:/var/www# vim old.html
Add the following text
OLD file

Step – 5 : Now create another file

root@linuxhelp:/var/www# vim new.html
Add the following text
NEW file

Step – 6 : Then make some changes in Apache default virtual host file

root@linuxhelp:/var/www# vim /etc/apache2/sites-available/000-default.conf
## Edit this line
DocumentRoot /var/www/
### Add this lines
	<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 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 – 8 : Restart the Apache2 service by using following command to apply new changes

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

Step – 9 : Now go to the browser and search with the following URL http://IP_ADDRESS/old.html?parameter1=value1

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 Ubuntu 22.04. Your feedback is much welcome.

FAQ
Q
How do I handle special characters in query strings when using Apache RewriteRule?
A
When working with query strings, you may need to escape special characters like `&` or `=`. You can use the
`%{QUERY_STRING}` variable and URL-encode any special characters as needed. For example:
RewriteRule ^old-url$ new-url?q=param1=%25amp%3Bencoded-value [R,L]
This will result in a rewritten URL like `new-url?q=param1=&encoded-value`.
Q
Can I modify a query string using Apache RewriteRule?
A
Yes, you can use the `RewriteCond` directive to modify a query string. For example:
RewriteCond %{QUERY_STRING} ^param1=old-value$ [NC]
RewriteRule ^old-url$ new-url?q=param1=new-value [R,L]
This will rewrite any URLs that match the original URL and have `param1` set to `old-value`, changing it to
`new-value`.
Q
How do I preserve the original query string when rewriting a URL using Apache RewriteRule?
A
You can use the `%{QUERY_STRING}` variable to capture the original query string and then append it to the
rewritten URL. For example:
RewriteRule ^old-url$ new-url?q=query-value%{QUERY_STRING} [R,L]
This will preserve any existing query strings and append them to the end of the rewritten URL.
Q
How do I add multiple query strings using Apache RewriteRule?
A
You can use the `&` character to separate multiple query strings. For example:
RewriteRule ^old-url$ new-url?q=param1&another-param=value [R,L]
This will result in a rewritten URL like `new-url?q=param1&another-param=value`.
Q
What is the syntax for rewriting a URL with a query string using Apache RewriteRule?
A
The basic syntax is:
RewriteRule ^old-url$ new-url?q=query-value [R,L]
Where `^old-url$` matches the original URL, `new-url` is the rewritten URL, `q=query-value` adds a query string to
the end of the rewritten URL, and `[R,L]` specifies that this rule should be executed last.